1 of 20

CSO101: Computer Programming

Lecture – 16

O.S.L. Bhavana

2 of 20

Multi-dimensional Arrays

  • Declaration of a multidimensional array should specify all the dimensions.
  • If the initialization is list initialization, only the left most dimension may be skipped. All the other dimensions are compulsory.
  • EX: Correct declarations:
    • int a[2][3] = {{1,2,4},{9,8,0}};
    • int a[][3][2] = {{{1,2},{2,3}}, {{3,6},{11,9}}}; // equivalent to declaring a[2][3][2]
    • int m=9, n=10; int a[n][m][n]; // declares a n*m*n matrix
  • Ex: Incorrect declarations
    • int a[2][] = {{1,2,4},{9,8,0}};// second dimension is missing
    • int a[][][2] = {{{1,2},{2,3}}, {{3,6},{11,9}}};// second dim missing

3 of 20

Multi-dimensional arrays as function arguments

  • All except the first dimension are compulsory to mention like in the declaration
  • If the array dimensions are constants, then they can be mentioned in the function declaration as follows

Ex: Function declaration: int max(int a[2][3]);

Function call: max(a);

Ex: incorrect function declaration: int max(int a[][]); //second dim missing

  • If the array dimensions are variables, then these variables can be declared in the function declaration too
  • Ex: Function declaration: int max(int x, int y, int a[x][y]); // here the dimensions of the 2D matrix are allowed to be variables. Note that the dimension variables x,y are declared before a.
  • Function Call: int x,y; scanf(“%d %d”,&x,&y); int a[x][y] max(x,y,a);

4 of 20

Multi-dimensional arrays as function arguments

  • Finding the max of elements of a 2Dmatrix

5 of 20

Storage Classes

  • Variables/identifiers consume memory
  • Some variables are only available for limited time
    • Variables declared within the loop statement – available only during loop execution
    • Variables declared within a function – available only during the function execution
    • Variables declared in main

6 of 20

Storage Classes

  • Various categories storage classes are available to specify availability of variables in the memory
  • C provides 4 storage classes
    • Auto
    • Register
    • Extern
    • Static

  • A storage class determines its storage duration, scope and linkage
  • Scope of a variable is where the variable can be referenced in a program
  • Storage duration is for how long the variable exists in the memory

7 of 20

Storage Classes

Feature

Auto

Register

Static

Extern

Storage

Stack

CPU Register

RAM

RAM

Initial Value

Garbage

Garbage

Zero

Zero

Scope

Local

Local

Local

Global

Lifetime

Block end

Block end

Program end

Program end

8 of 20

Scope refers to the region of the program where a variable is "visible" or accessible by its name.

  • Block Scope (Local): Variables declared inside a function or a block { }. They are only accessible within that specific block.
  • File Scope (Global): Variables declared outside of all functions. They are accessible from the point of declaration until the end of the file.
  • Function Prototype Scope: Variables (parameters) used in a function declaration (e.g., int add(int a, int b);). These names only exist for that specific line.

Lifetime refers to the duration of time for which a variable remains in the computer's memory during program execution.

  • Automatic Lifetime: Variables are created when the block is entered and destroyed when the block is exited (typical for auto and register).
  • Static Lifetime: Variables are created when the program starts and destroyed only when the program terminates (typical for static and extern).
  • Dynamic Lifetime: Variables created manually using memory allocation functions like malloc(). They stay in memory until you manually free() them.

9 of 20

Auto

  • A variable declared with auto keyword is destroyed when its enclosing block is exited
  • EX:
  • main()
  • {{auto int a=10;} printf(“%d”, a); } // results in error. a has to be used within the block its declared
  • Auto is the default storage class and can be omitted.
  • A variable may be re-declared in different scopes. Re-declaration within same scope results in compilation errors.
  • EX: {int a=10; {int a=900; printf(“%d”,a);}printf(“%d”,a);} outputs 900, 10
  • Auto variables may be accessed outside their block using pointers! (later)

10 of 20

Register

  • register int a =1;
  • Similar to auto
  • The variable is stored in the CPU register (not in the memory) for fast access
  • If CPU registers are not available for storage, then the variable may also be stored in the memory.
  • Some compilers are clever enough to identify frequently used variables and store them automatically in the registers
  • Address of register variable cannot be accessed (can’t use scanf!!)

11 of 20

Static Storage Duration

  • To declare variables that exist throughout the program the keywords extern or static need to be used
  • Variables with static storage duration exist till the end of the program.
  • Even functions have static storage declaration

12 of 20

Static Storage Duration: Global Variables

  • Global variables are declared outside the function descriptions
  • extern is the default keyword for such variables
  • Global variables and functions can be referenced by any function that follows their declaration or definition in the file.
  • Need to be cautiously used; as many functions can access the same variable
  • #include <stdio.h>
  • int a=9;

void f(){......}

void g(){......}

main(){....} // variable a can be used by f, g and main

13 of 20

Extern & global variables

int x; // assigns memory for x

int y = 10; //assigns memory for y and sets the value

extern int z; // doesn’t assign memory. Tells the compiler that z is somewhere else (in the header files etc)

int main()

{…..}

int f()

{.....} // both main and f can access x,y,z

All x,y,z are extern storage class variables

14 of 20

Extern

  • Variables declared as extern have File Scope. Once declared, they are visible to all functions within that file. If defined in file1.c and declared as extern in file2.c, both files share the exact same memory location for that variable.
  • An extern variable has a Static Lifetime.
  • In C, all functions are extern by default.
  • Cannot initialize an extern variable inside a function; it must be done at the file level (globally).
  • If not explicitly initialized, extern variables are automatically set to zero.

15 of 20

Static Storage Duration: Global Variables

  • Global variables are declared outside the function descriptions
  • extern is the default keyword for such variables
  • extern keyword can be used to access variables/functions across different files
  • Ex: - int foo(int arg1, char arg2);�is treated as - extern int foo(int arg1, char arg2);

which tells the compiler that there exists a function with name foo and its definition is somewhere else�

16 of 20

Static Storage Duration: Static Keyword

  •  static int count = 1;
  • Static variables are local to the function in which they are declared but retains its value even after  the function is exited.
  •  All numeric variables of static storage duration are initialized to zero if they are not explicitly initialized.
  • But initialization is done only for the first time (not on subsequent function calls)
  • means that internal static variables provide private, permanent storage within a single

17 of 20

Static Keyword

 void f_1(void);

 int main( )

 {func(); func();func(); } // prints 1,2,3

 void func(void)

{

   static int count = 1;

   printf(“%d\n”, count);

   count ++;

 } 

18 of 20

Static Keyword

 void f_1(void);

 int main( )

 {f_1(); f_1();f_1(); } // prints 1,1,1

 void f_1(void)

{

   static int count = 1; count=1;

   printf(“%d\n”, count);

   count ++;

 } 

Useful in counting the number of recursive calls a function has

19 of 20

Static Keyword

  • Useful in counting the number of recursive calls

int fib(int n) //function to print the n^th fibonacci number

 {

static int counter = 0; 

counter ++;

printf(“In fib with counter = %d \n”, counter);

if (n==0 || n==1) return n;

else return fib(n-2)+ fib(n-1);

 }

Number of recursive calls in fib(4)?

20 of 20

Shadowing

Variables with same name can exist across different scopes.

int global_var = 60;

void myFunction() {

int x = 10;

static int y = 40; // Outer x

if (1) {

int x = 50; // Inner x (Shadowing)

int y = 50;

int global_var= 50;

printf("%d %d %d ", x,y,global_var);

}

printf("%d %d %d ", x,y,global_var);

}

int main()

{

printf("Hello World");

myFunction();

return 0;

}