Ch. 7 Storage Slass and Program Structure

Ch 7 Storage Slass - Storage Classes and Program Structure Outline Introduction Global and Local Variables Scope of Identifier Storage Class Static

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Storage Classes and Program Structure Outline Introduction Global and Local Variables Scope of Identifier Storage Class Static Variables Outside Function Static Variables Inside a Function External Variables and Functions Run Programs with Multiple Files Global and Local Variables Although it is not recommended, global variables shared by different functions can be used to communicate between functions. The same identifiers can be used in different scopes. The values of the variables are stored in different memory locations. /* File: global.c */ #include <stdio.h> int g = 10; // global variable void func1() { g++; printf("g in func1() = %d\n", g); } void func2() { g++; printf("g in func2() = %d\n", g); } void func3() { int g=0; // local variable g++; printf("g in func3() = %d\n", g); } int main() { func1(); func2(); func3(); printf("g in main() = %d\n", g); return 0; } Output: g in func1() = 11 g in func2() = 12 g in func3() = 1 g in main() = 12 Scope of Identifiers The scope of an identifier is the portion of the program in which the identifier can be accessed. There are four types of scope: program scope , file scope, function scope , and block scope . – Program scope . The identifiers having a program scope are accessible among different files. Variables with program scope are called global variables . – File scope . The identifiers having a file scope are active from its declaration point to to the end of the file. The global static variables have a file scope. – Function scope . The identifiers having a function scope are active from the beginning to the end of the function. The variables declared at the beginning of a function have a function scope. – Block scope . A block is a bunch of statements enclosed in braces. The identifiers having a block scope is active from its declaration point to the end of the block in which it is declared. The variables declared inside a block have a block scope. Scope of Identifiers Program Scope File Scope Function Scope Block Scope Block Scope Function Scope Example: Output: program_i in main() = 10 file_i in main() = 20 program_i in func() = 10 file_i in func() = 20 function_i in func() = 30 program_i in block = 10 file_i in block = 20 function_i in block = 30 block_i in block = 40 /* File: scopeid.c */ #include <stdio.h> int program_i = 10; /* extern int otherfile_i; if otherfile_i is declared in other file */ static int file_i = 20; void func() { int function_i = 30; printf("program_i in func() = %d\n", program_i); printf("file_i in func() = %d\n", file_i); printf("function_i in func() = %d\n", function_i); { int block_i = 40; printf("program_i in block = %d\n", program_i); printf("file_i in block = %d\n", file_i); printf("function_i in block = %d\n", function_i); printf("block_i in block = %d\n", block_i); } } int main() { printf("program_i in main() = %d\n", program_i); printf("file_i in main() = %d\n", file_i); func(); return 0; } Problem Statement: A projectile is fired into the air with an initial speed v0 of...
View Full Document

This note was uploaded on 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.

Page1 / 27

Ch 7 Storage Slass - Storage Classes and Program Structure Outline Introduction Global and Local Variables Scope of Identifier Storage Class Static

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online