Lec35-36 - Computer Programming I TA C162. To be discussed...

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

View Full Document Right Arrow Icon
1 Computer Programming I TA C162 . 17 Apr 2007 To be discussed today…. . Variable Storage classes in C Scope rules visibility and life time Examples  
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Computer Programming I TA C162 . 17 Apr 2007   Variable storage classes in C  Automatic Variables  External Variables  Static Variables  Register Variables Broad classification of variables: Local (Internal) Global (External) Note: Concept useful in multifunction program
Background image of page 2
3 Computer Programming I TA C162 . 17 Apr 2007 More about variable storage classes The class of a variable may be either permanent  or temporary.  Global variables are always permanent. They are created and  initialized before the program starts and remain until it  terminates.  Temporary variables are allocated from a section of memory  called the stack at the beginning of the block.  Each time the block is entered, the temporary variables are  initialized.  Local variables are temporary unless they are declared  static .
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Computer Programming I TA C162 . 17 Apr 2007 Scope, Visibility and Lifetime of Variables Scope  determines over what region of the program a variable  is  active. Lifetime  refers to the period during which a variable  retains  a  given value. Visibility  refers to the  accessibility  of a variable from the  memory. Inference: Visibility of the variable is within the scope only
Background image of page 4
5 Computer Programming I TA C162 . 17 Apr 2007 How automatic variables work? void fun1(void) { int x=25; fun2(); printf(“%d\n”,x); } void fun2(void) { int x=5; printf(“%d\n”,x); } void main() { int x=125; fun1(); printf(“%d\n”,x); } Execution completed Output: 5 25 125
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Computer Programming I TA C162 . 17 Apr 2007 How global variables work? int val ; int fun1(void) { val += 5; return val ; } int fun2(void) { int val =0; return val ; } int fun3(void) { val +=5; return val; } void main() { val = 5; printf(“val= %d\n”,val); printf(“val= %d\n”,fun1()); printf(“val= %d\n”,fun2()); printf(“val= %d\n”,fun3()); } Output: val=5 val=10 val =0 val=15
Background image of page 6
7 Computer Programming I TA C162 . 17 Apr 2007
Background image of page 7

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

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

Page1 / 25

Lec35-36 - Computer Programming I TA C162. To be discussed...

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