90 likes | 92 Views
This lecture covers the use of header files, including function prototypes, data types, and constants, as well as the concepts of call by value and call by reference in C programming. It also explains the scope rules for identifiers in different scopes.
E N D
Headers • Header files: Each standard library has a corresponding header. • The function prototype for all the library functions. • Definition of various data types • Definition of constants needed by the library functions • Load with #include<filename> #include<math.h> • Custom header files • Including function prototypes, definition of data types and constants. • Save as filename.h • Load with #include“filename.h” • Source file and filename.h are in the same directory. • Purposes: Reuse functions
memory memory memory memory … … … … 5 a 5 a 5 5 a a … … … … x 15 x 5 … … … … Calling Functions • Call by value • Copy of argument passed to function • Changes in function do not effect original • Use when function does not need to modify argument • To avoid accidental changes #include<stdio.h> int add10(int x); int main( void ) { int a = 5; add10(a); printf(“%d\n”, a); return 0: } int add10(int x) { x += 10; return x; } when calling add10() Before calling add10() Just after return from add10() Just before return from add10()
Calling Functions • Call by reference • Passes original argument • Changes in function effect original • Only used with trusted functions • In C, all calls are by value. • Possible to simulate call-by-reference in C
Scope Rules • The scope of an identifier is the portion of the program in which the identifier can be referenced. • File scope • Identifier defined outside function, know in all functions from the point at which the identifier is declared until the end of the file. • Used for global variables, function definitions, function prototypes
Scope Rules • Block scope • Identifier declared inside a block • Block scope begins at definition, ends at the terminating right brace ({) of the block. • Used for local variables, function parameters • Outer blocks “hidden” from inner blocks if there is a variable with the same name in the inner block
Scope Rules - Example #include<stdio.h> void useGlobal( void ); void useLocal( void ); int x = 1; /* global variable */ int main( void ) { int x = 5; printf("x on entering main is %d\n", x); {/* start new scope */ int x = 7; /*local variable to new scope */ printf("x in inner scope of main is %d\n", x); }/* end new scope */ printf("x in outer scope of main is %d\n", x); useLocal(); useGlobal(); useLocal(); useGlobal(); printf("\nx on exiting main is %d\n", x); return0; } void useLocal(void) { int x = 25; printf("\n x is %d on entering useLocal\n", x); x++; printf(" x is %d on exiting useLocal\n", x); } void useGlobal(void) { printf("\n x is %d on entering useGlobal.\n", x); x *= 10; printf(" x is %d on exiting useGlobal.\n", x); }
Scope Rules • Function prototype scope • Identifiers used in the parameter list of a function prototype. • Variable names are ignored by the compiler • Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity. • Function scope • Can only be referenced inside a function body. • Used only for labels (start:, case:, etc.)
Practice Question Q. What is the output of the following program? #include<stdio.h> void anotherGlobal( void ); void useGlobal( void ); int x = 1; /* global variable */ int main( void ) { anotherGlobal(); printf(”%d ", x); return0; } void anotherGlobal(void) { int x = 5; useGlobal(); printf(“%d ”, x); } void useGlobal(void) { x *= 10; printf(”%d ", x); } 5 10 10 10 10 5 10 5 10 10 5 1 Solution: D