A function has to be declared before it is used int i for i 0 i 10 i printidni

A function has to be declared before it is used int i

This preview shows page 12 - 16 out of 33 pages.

In C functions cannot be defined within other functions. A function has to be declared before it is used. int i; for (i = 0; i < 10; i++) { print("i=%d\n",i); } ------------------------ int i = 10; while (i > 0) { print("i=%d\n",i); i--; } ------------------------ int i = 10 do { print("i= %d\n",i); i--; } while (i > 0) for (...) { for (...) { ... if (disater) goto LABEL_1; } } LABEL_1: ...
Image of page 12
4 int main() 5 { int x = 10; int divisor = 2; printf("%d mod %d = %d\n", x, divisor, modulo(x,divisor, 0 )); divisor = 3; printf("%d mod %d = %d\n", x, divisor, modulo(x,divisor, 1)); return 0; 14 } 15 16 /* c o m p u t e s the r e m a i n d e r from d i v i d i n g x by d i v i s o r */ 17 int modulo( int x, int 18 { if ( !rec ) { /* i t e r a t i v e v e r s i o n */ while (x >= divisor) { x -= divisor; 23 } 24 return x; 25 } else { /* r e c u r s i v e v e r s i o n */ if (x < divisor) return x; else return modulo(x - divisor, divisor, rec); 32 } 33 } We can also create functions in multiple files. If functionA uses functionB which is defined in another file, then one of the following has to be true: functionB is declared in a file that defines functionA (before functionB is used) the file that contains functionA includes a header file that declares functionB (we will look at header files soon) 3.5 Variable scope Variable Scope: global/external variables Scope rules: The scope of a variable/function name is the part of the program within which the name can be used A global (external) variable or function’s scope lasts from where it is declared to end of the file A local (automatic) variable’s scope is within the function or block int x; void foo( int y) { y++; x++; / * x is a c c e s s i b l e b e c a u s e it is in g l o b a l s c o p e * / }
Image of page 13
void bar() { y = 1; / * w r o n g - t h e r e is no y in this s c o p e * / x = 1; / * x is a c c e s s i b l e b e c a u s e it is in g l o b a l s c o p e * / } Demo ( scope.c ) : What if we change all y ’s to x ’s? Global/Local Variable Initialization In the absence of explicit initialization global and static variables are guaranteed to be initialized to zero local variables have undefined initial value !!! Definition vs. declaration of a global (external) variable Declaration specifies the type of a variable. Definition also set aside storage for the variable and initializes its value. The following example uses an external/global variable and a function both defined in a file different than main function. void foo( int y) { if (y > 10) { int i; for (i = 0; i < y; i++) { ... } } else { y++; // ’ i ’ is out of s c o p e here } } add.c -------------------- int counter = 0; void add_one() { counter++; } program.c -------------------- #include <stdio.h> extern int counter; void add_one(); int main() { printf("counter is %d\n", counter); add_one(); add_one(); printf("counter is %d\n", counter);
Image of page 14
return 0; } The two file program can be compiled using or using separate compilation (each file compiled individually) gcc -c -Wall -g add.c gcc -c -Wall -g program.c gcc add.o program.o -o program WARNING: Avoid global/external variables (constants are fine)! They are considered bad programming style. Use of globals results in programs that are hard to debug and trace: just imagine how many different functions can possibly be modifying the value of a global variable. static keyword The keyword static limits a scope of a global variable to within the rest of the source file in which it is declared. If we add the keyword static to the declaration of counter in add.c above, the
Image of page 15
Image of page 16

You've reached the end of your free preview.

Want to read all 33 pages?

  • Summer '19
  • GCC

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture