









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Module 2 Dr. P.Keerthika Associate Professor School of Computer Science & Engineering VIT,Vellore.
BCSE102L- Structured and object-oriented programming
⚫ Module-2:ARRAYS AND FUNCTIONS ◦ Arrays – 1D & 2D ◦ Strings and its operations ◦ User defined Functions 🞄 Declaration and Definition 🞄 Call by Value and Call by reference 🞄 Types of Functions- Recursive Functions ◦ Storage Classes 🞄 Scope,Visibility and lifetime of variables
int a; void main() { char b ; { float c; } } Scope of a Visibility of a Lifetime of a Scope of b Visibility of b Lifetime of b Scope of c Visibility of c Lif
⚫ Storage class of a variable tells the compiler ◦ Storage area of the variable ◦ Initial value of the variable if not initialized ◦ Scope of the variable ◦ Life of the variable that is how long the variable would be active in the program. ⚫ Four Categories ◦ Automatic Variables ◦ Static Variables ◦ External Variables ◦ Register Variables
External Variables ⚫ Variables that are active throughout the entire program are called as external variables ( global variables). External variables are declared outside the function body. ⚫ Variable retain the value throughout the execution of a program. This storage class can be accessed by any function in same or different program file and change its value. int number; float length=6.2; main() { } function1() { } function2() { }
External Variables int fun1(void); // Other way of declaring nil arguments int fun2(void); int x; /GLOBAL/ main() { x=10; /GLOBAL / printf(“x=%d\n”, x); printf(“x=%d\n”, fun1()); printf(“x=%d\n”, fun2()); } Int fun1(void) { x=x+10; /GLOBAL/ } int fun2(void) { int x ; /LOCAL/ x=1 ; return(x) ; }
Static Variables ⚫ Static variable may be either an internal type or an external type depending on the place of declaration. ⚫ Global and Local variable can be declared static. Static variables are initialized only once when they are compiled in a program. ⚫ Variable can be declared using the keyword static. ⚫ Internal Static Variables - They are local to the block in which declared. It exists until the end of the program. ( Note: Like an auto variables ). When the program is closed the function associated with that program is also excited and whenever it is visited again the same value exists. ⚫ External static variable is declared outside of all functions. It is made available to all functions in a C program.
Static Variables void start(void); void main() { int i; for(i=1;i<3;i++) start(); } void stat(void) { static int x=0; // Initialized once x=x+1; printf(“x=%d\n”,x); } Output??
Register Variables void main() { register int x; for(x=1;x<=10;x++) printf(“%d”,x); printf(“%u”,&x); // Not able to get the address } int main() { int i = 10; register int a = &i; printf("The value of pointer : %d",a); return 0; }