ccplusplus.com
Learn C, C++ Concepts
Sunday, December 25, 2011
external variable example in c
/*********************************************************************** * File : external-variable.c * Author : Saurabh Gupta * Desc : external variable in C * Source : http://ccplusplus.com/p/c.html * Created : PM 04:14 25 December 2011 * Note : ************************************************************************/ #include
void next (void); void next1 (void); /* * a1 : external variable:- global scope * scope : main, next, next1 */ int a1 = 1; int main () { printf ("*** Scope of External Variables ***\n"); a1 = 2; printf ("a1 = %d\n", a1); // a1 = 2 next (); printf ("a1 = %d\n", a1); // a1 = 2 next1 (); printf ("a1 = %d\n", a1); // a1 = 3 return 0; } /* * b1 : external variable * scope : global to next, next1 * main, cannot access b1 */ int b1 = 0; void next (void) { /* * a1 : auto var:- scope local to next * next cannot access external a1 */ char a1; a1 = 'a'; b1 = 77; // external variable printf ("a1 = %c, b1 = %d\n", a1, b1); // a1 = a, b1 = 77 } void next1 (void) { /* * b1 : auto var:- scope local to next1 * next1 cannot access external b1 */ float b1; b1 = 19.2; a1 = 13; // external variable printf ("a1 = %d, b1 = %f\n", a1, b1); // a1 = 13, b1 = 19.2 } /* * * [sgupta@rhel6x64 c]$ gcc external-variable.c -o external-variable [sgupta@rhel6x64 c]$ ./external-variable *** Scope of External Variables *** a1 = 2 a1 = a, b1 = 77 a1 = 2 a1 = 13, b1 = 19.200001 a1 = 13 [sgupta@rhel6x64 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment