0% found this document useful (0 votes)
5 views8 pages

Storage Class Specifier

The document explains storage class specifiers in C, detailing how they control a variable's storage, scope, and lifetime. It covers different storage classes such as auto, register, static, and extern, along with their characteristics and usage. Additionally, it introduces 2D arrays in C, including their declaration, initialization methods, and traversal.

Uploaded by

sachhusachin277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Storage Class Specifier

The document explains storage class specifiers in C, detailing how they control a variable's storage, scope, and lifetime. It covers different storage classes such as auto, register, static, and extern, along with their characteristics and usage. Additionally, it introduces 2D arrays in C, including their declaration, initialization methods, and traversal.

Uploaded by

sachhusachin277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

storage class specifier :

they control how a variable behaves in terms of

• storage :where the variable is stored


• scope: where the variable accessible (stack,data segment,registers)
• lifetime:how long the variable exists in the program

stack:
it is part of RAM
managed by cpu during program execution
it stores :

• function calls
• function arguements
• local variables
• return addresses

cpu registers:
• they are small ,high speed memory location,in processor ,that is used to
store temporary data
• accessing data from register is much more faster than accessing it from
ram
• it speeds up the execution

AUTO STORAGE CLASS SPECIFIER


• the default storage class in C for all local variables declared inside the
function or block.
• it implicitely applied meaning if you do not specify it automatically treats
it as auto
• storage : stored in stack
• scope : local to that block/function
• lifetime: variables are created when function is created(stack frame also
creates) and destroyed when function ends its function
• for uninitialized variable :takes garbage value
• cannot be accesses outside block .
SYNTAX: auto datatpe var_name;

Output:

Register

• it is the storage class which is suggest to make variable to store in


registers instead of accessing it via ram
• the compiler may ignore this suggestion if none of the registers are
available and stores it in RAM
• if registers are free it allocates the memory for variables in register .
register datatype num;
Output:

static storage class specifier


modifies the scope and lifetime of variable

• it is used retain the value of variable across the function calls and
restricts its scope inside file only,
• what is meaning of retain :when we say retain means the value is not
lost when function which was created ends.
• the function's variable remembers the last value it stored
storage area:data segment
scope: it depends on where it is declared if it is declared in function it has
fucntion scope
outside function: file scope
when a program runs it uses the memory that is divided into

• code segment,
• data segment,
• heap
• stack
the memory is allocated at program startup and takes throughout program
execution
data segment :

• initialized Data segment : stores static variable with explicit value


• uninitialized data segment : stores static variable with out any
explicit value (with garbage value 0)

Output:

Extern storage class specifier

• Extern is a keyword used to declare a variable or function without


defining it.
• It tells the compiler that the actual definition exists somewhere else
(usually in another file).
• It is mainly used for global variable access across multiple files.
• Scope: global (accessed anywhere if declared properly)
• Lifetime:entire program execution
• Storage location: data segment
Program:

Program that caluculates frequency of the element in the array


A 2D array (Two-Dimensional Array) in C is a collection of elements stored in
rows and columns. It is essentially an array of arrays, where each row itself is a
1D array.
A 2D array in C is declared using the following syntax:
c
Copy code
data_type array_name[rows][columns];
• data_type: The type of data to be stored (e.g., int, float, char, etc.).
• array_name: The name of the array.
• rows: Number of rows.
• columns: Number of columns.
Example: Declaration
c
Copy code
int matrix[3][4]; // A 2D array with 3 rows and 4 columns

2 Declaration and Initialization of a 2D Array


A 2D array can be initialized in different ways.
Method 1: Row-wise Initialization
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Here:
• matrix[0] → {1, 2, 3}
• matrix[1] → {4, 5, 6}
• matrix[2] → {7, 8, 9}
Method 2: Omitting Row Size
You can omit the row size while initialization:
int matrix[][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
The compiler automatically determines the number of rows.
Method 3: Single-Line Initialization
int matrix[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
The elements are stored row-wise.
Traversing:

(try prompting user)


Minimum element the 2D array:

You might also like