CS 24000 Lecture Notes - Lecture 9: Local Variable, Global Variable

58 views2 pages

Document Summary

Scope and lifetime of a variable: scope = the place in the program where a variable can be used, lifetime = the time in the execution when a variable is valid. If the c program spans more than two files a global variable can be used in both files: one file has the definition. Int total: the other file may have an extern declaration to tell that the variable is defined in another file. Extern int total: the extern declaration in this case is optional but recommended, global variables are initialized with 0s. Global variables in multiple files sum. c: extern int total; // extern declaration void sum( int a, int b) { total = a + b; main. c: #include int total; //definition extern sum(int a, int b); main() { sum(5,8) printf( sum=%d\n , total); The initial variable will be whatever value is in the stack when the function starts.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents