0% found this document useful (0 votes)
6 views16 pages

Program Structure Storage Classes

The document provides an overview of the structure of C programs, detailing various sections such as documentation, link, definition, global declaration, main function, local declaration, executable section, and user-defined functions, emphasizing their roles in program execution. It also explains storage classes in C, including automatic, external, static, and register, highlighting their characteristics and usage in managing variable scope, lifetime, and memory. Understanding these concepts is essential for writing efficient and well-structured C programs.

Uploaded by

amitsingkumar981
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)
6 views16 pages

Program Structure Storage Classes

The document provides an overview of the structure of C programs, detailing various sections such as documentation, link, definition, global declaration, main function, local declaration, executable section, and user-defined functions, emphasizing their roles in program execution. It also explains storage classes in C, including automatic, external, static, and register, highlighting their characteristics and usage in managing variable scope, lifetime, and memory. Understanding these concepts is essential for writing efficient and well-structured C programs.

Uploaded by

amitsingkumar981
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

Program Structure in C – Introduction

A C program structure refers to the systematic arrangement of different parts of a program


so that it can be easily written, understood, compiled, and executed. It provides a logical flow to
the program, ensuring that all instructions are placed in the correct order. A well-defined
structure improves readability, debugging, and maintenance of the code. In C, programs are
divided into multiple sections such as documentation, link, definition, global declaration, main
function, and subprograms. Each section has its own specific role and importance in program
execution. The structure helps the compiler to process the program step-by-step efficiently. It
also supports modular programming, where large problems are broken into smaller functions.
Proper program structure makes it easier for multiple programmers to work on the same project.
It ensures that variables and functions are declared in the right scope. Overall, program structure
is the foundation of writing clean, efficient, and error-free C programs.

Complete Example with All Sections


#include <stdio.h> // Link section

#define MSG "Hello" // Definition section

int global = 10; // Global declaration

void show(); // Function declaration

int main() // Main function


{
int a = 5; // Local declaration

printf("%s World\n", MSG); // Executable section


show(); // Function call

return 0;
}

void show() // User-defined function


{
printf("Global = %d", global);
}

Program Structure Sections

1. Documentation Section
The documentation section contains comments that describe the program.
It helps in understanding the purpose and logic of the code.
Comments are ignored by the compiler during execution.
It improves readability and maintenance of the program.
Both single-line and multi-line comments can be used.

✔ Example:
// This program prints message and global value

2. Link Section

The link section is used to include header files in the program.


It allows the use of predefined functions like printf and scanf.
It is written using the #include directive.
This section is processed before compilation starts.
It is always written at the top of the program.

✔ Example:
#include <stdio.h>

3. Definition Section

The definition section is used to define constants using macros.


It is written using the #define directive.
The value is replaced before compilation begins.
No memory is allocated for defined constants.
It improves readability and easy modification of values.
✔ Example:
#define MSG "Hello"

4. Global Declaration Section

Global variables are declared outside all functions.


They can be accessed by all functions in the program.
Their lifetime is throughout the program execution.
The default value of global variables is zero.
They are used to share data between functions.

✔ Example:
int global = 10;
5. Main Function Section

The main function is the starting point of the program.


Execution of the program begins from this function.
Every C program must contain one main function.
It can contain declarations and executable statements.
It returns a value to the operating system.

✔ Example:
int main() {

6. Local Declaration Section

Local variables are declared inside a function.


They can be used only within that function.
Their lifetime is limited to function execution.
They do not affect other parts of the program.
They are stored in stack memory.

✔ Example:
int a = 5;

7. Executable Section

This section contains statements that perform operations.


It includes input, output, and processing instructions.
These statements are executed line by line.
It represents the logic of the program.
Without it, the program will not perform any task.

✔ Example:
printf("%s World\n", MSG);

8. User-defined Functions

User-defined functions are created by programmers.


They are used to perform specific tasks.
They help in reducing code repetition.
They improve modularity of the program.
They can be called from the main function.
✔ Example:
void show() {
printf("Global = %d", global);
}

Storage Classes in C

Introduction

Storage classes in C define the scope, lifetime, visibility, and memory location of variables.
They tell the compiler where to store a variable, how long it will exist, and who can access
it. Every variable in C belongs to a storage class, either by default or explicitly declared. Storage
classes help in managing memory efficiently and controlling variable usage in large
programs. They are especially useful in modular and multi-file programs. By using storage
classes, we can avoid unnecessary memory usage and protect data from unwanted access.
The main storage classes in C are automatic (auto), external (extern), static (static), and register
(register). Understanding storage classes is essential for writing optimized and well-structured
programs.

Every variable in C has a storage class, whether specified or not.


Storage classes help in managing memory efficiently.
They are very important for controlling the behavior of variables in programs.

There are:

[Link] Variables

Local variables are created when the function starts executing and destroyed when the
function ends. They are private to the function, meaning no other function can access them.
These variables are stored in stack memory, which is automatically managed by the system. By
default, local variables contain garbage values if not initialized. They help in avoiding conflicts
between variables of different functions. Local variables make programs more secure because
their data is not accessible globally. They are widely used in modular programming where each
function has its own data.

Characteristics:

 Scope → Inside function/block only


 Lifetime → Till function execution
 Default value → Garbage value
 Storage → Stack memory
 Accessibility → Only within function
✔ Example:
#include <stdio.h>

int main() {
int a = 10; // Local variable
printf("%d", a);
return 0;
}

2. Global Variables

Global variables are created when the program starts and destroyed when the program ends.
They are stored in the data segment of memory. These variables are accessible throughout the
program, making them useful for sharing data between functions. By default, global variables are
initialized to zero. However, excessive use of global variables can make programs complex and
harder to debug. They should be used carefully to maintain program clarity and security. Global
variables are especially useful in large programs and multi-function applications.

Characteristics:

 Scope → Entire program


 Lifetime → Whole program execution
 Default value → 0
 Storage → Data segment
 Accessibility → All functions

✔ Example:
#include <stdio.h>

int x = 50; // Global variable

int main() {
printf("%d", x);
return 0;
}
Types of Storage Classes in C

1. Automatic Storage Class (auto)


✔ Definition

Automatic storage class is the default storage class for all local variables in C.
When a variable is declared inside a function or block, it is automatically considered as auto.

✔ Detailed Theory

Automatic variables are created when a function or block starts executing and destroyed
when it ends. Their scope is limited to the block in which they are declared. These variables do
not retain their values after the function ends. If not initialized, they contain garbage values.
They are stored in stack memory, which is automatically managed. The keyword auto is optional
because all local variables are auto by default. These variables are mainly used for temporary
calculations inside functions.

✔ Syntax

auto data_type variable_name;


✔ Example:
auto int x;

✔ Characteristics

 Scope → Within function/block only


 Lifetime → Till function execution
 Default value → Garbage value
 Storage → Stack memory
 Keyword → Optional

Example Program

#include <stdio.h>

void display() {
auto int x = 10; // automatic variable
printf("Value of x: %d\n", x);
}

int main() {
display();
return 0;
}

✔ Output
Value of x: 10
Short Explanation

 #include <stdio.h> is used for printf() function.


 display() is a user-defined function.
 Inside it, auto int x = 10; declares an automatic variable.
 This variable exists only inside display() and is destroyed after the function ends.
 printf() prints the value of x as 10.
 In main(), the function display() is called.

Sum of Two Numbers (auto)

✔ Program:

#include <stdio.h>

int main() {
auto int a = 5; // automatic variable
auto int b = 10; // automatic variable
auto int sum; // automatic variable

sum = a + b;

printf("Sum = %d", sum);

return 0;
}

✔ Output:

Sum = 15

✔ Simple Explanation:
 a, b, and sum are automatic variables
 They are created when main() starts
 They are destroyed when main() ends
 Their scope is only inside main()

Even if you remove auto, it will still work because local variables are auto by default

2. External Storage Class (extern)

✔ Definition

External storage class is used to declare a global variable that is defined somewhere else.
It allows variables to be shared between different functions or even different files.

✔ Detailed Theory

The extern keyword is used when we want to access a global variable in another function or file.
It does not create a new variable, it only tells the compiler that the variable is already defined
somewhere. External variables have global scope and lifetime, meaning they exist throughout
the program execution. By default, their value is 0 if not initialized. They are stored in the data
segment of memory. This storage class is very useful in large programs and multiple file
programs where data needs to be shared.

✔ Syntax

extern data_type variable_name;


✔ Example:
extern int x;

✔ Characteristics

 Scope → Global (can be used in different iles/functions)


 Lifetime → Entire program
 Default value → 0
 Storage → Data segment
 Keyword → Used for declaration, not de inition
✔ Easy Example (Single File)
#include <stdio.h>

int x = 20; // global variable

int main() {
extern int x; // accessing global variable
printf("Value of x = %d", x);
return 0;
}

✔ Output:

Value of x = 20

✔ Explanation:

 x is declared outside → global variable


 extern int x; is used inside main()
 It tells compiler to use the same global x
 No new variable is created

✔ Easy Sum Example (extern)


#include <stdio.h>

int a = 5, b = 10; // global variables

int main() {
extern int a, b;
int sum;

sum = a + b;

printf("Sum = %d", sum);

return 0;
}
✔ Output:

Sum = 15
Key Points to Remember

✔ extern is used to access global variables


✔ It does not create memory
✔ Useful in multiple file programs
✔ Helps in sharing data
✔ Improves modular programming

3. Static Storage Class (static)

✔ Definition

Static storage class is used to retain the value of a variable between function calls.
It means the variable is created only once and its value is not destroyed after function execution.

✔ Detailed Theory

Static variables are initialized only once and stored in the data segment of memory. Their
lifetime is the entire program, but their scope depends on where they are declared. If declared
inside a function, they behave like local variables but retain their value. If declared outside
(global), their scope is limited to that file only. By default, static variables are initialized to 0. They
are very useful when we want to preserve data between multiple function calls without using
global variables.

Types of Static Variables

1. Static Local Variable


2. Static Global Variable
1. Static Local Variable

✔ Theory:

A static local variable is declared inside a function.


It keeps its value even after the function ends.
It is initialized only once.
Its scope is limited to the function.
It is useful for counting or storing previous values.

✔ Example:

#include <stdio.h>

void counter() {
static int count = 0; // static local variable
count++;
printf("Count = %d\n", count);
}

int main() {
counter();
counter();
counter();
return 0;
}

✔ Output:

Count = 1
Count = 2
Count = 3

Explanation:

 count is declared inside function → local scope


 static makes it retain value between function calls
 It is initialized only once
 Value is not reset to 0 each time
2. Static Global Variable

✔ Theory:

A static global variable is declared outside all functions.


Its scope is limited to the same file only.
It cannot be accessed from other files.
Its lifetime is the entire program.
It is used to restrict data access.

✔ Example:

#include <stdio.h>

static int x = 10; // static global variable

void show() {
printf("Value of x = %d\n", x);
}

int main() {
show();
return 0;
}

Output:

Value of x = 10

Explanation:

 x is declared outside → global variable


 static restricts its scope within the same file only
 It cannot be accessed from other files using extern
 Lifetime is entire program
✔ Easy Sum Example (Static)
#include <stdio.h>

void sum() {
static int a = 5, b = 10;
static int s;

s = a + b;
printf("Sum = %d\n", s);
}

int main() {
sum();
sum();
return 0;
}

✔ Output:

Sum = 15
Sum = 15

Values remain same (not reinitialized)

Characteristics of Static Variables

 Scope → Local or ile-level


 Lifetime → Entire program
 Default value → 0
 Storage → Data segment
 Value persists between function calls

Key Points to Remember

✔ Initialized only once


✔ Value is retained
✔ Stored in data segment
✔ Helps avoid global variables
✔ Useful in counters, memory saving

4. Register Storage Class (register)

✔ Definition

Register storage class is used to store variables in CPU registers instead of main memory (RAM)
for faster access.
It is mainly used for variables that are frequently used, like loop counters.

✔ Detailed Theory

Register variables are stored in the CPU register, which makes their access very fast compared to
normal variables. However, storing in a register is only a request to the compiler, not a guarantee.
If registers are not available, the variable may be stored in memory. These variables have local
scope and exist only during function execution. One important limitation is that we cannot use
the address operator (&) with register variables. They are mainly used in loops and performance-
critical code. Their default value is garbage if not initialized.

✔ Syntax

register data_type variable_name;


✔ Example:
register int x;

✔ Characteristics

 Scope → Local (within function)


 Lifetime → Function execution
 Default value → Garbage value
 Storage → CPU Register (if available)
 Cannot use & operator
✔ Easy Example
#include <stdio.h>

int main() {
register int i;

for(i = 1; i <= 5; i++) {


printf("%d ", i);
}

return 0;
}

✔ Output:

12345

✔ Explanation:

 i is declared as register variable


 Used in loop → accessed frequently
 Faster execution compared to normal variable

✔ Important Note (Very Important for Exam)

❌This is NOT allowed:

register int x = 10;


printf("%d", &x); // ❌Error

Because register variables do not have memory address accessible

✔ Easy Sum Example (Register)


#include <stdio.h>

int main() {
register int a = 5, b = 10;
int sum;

sum = a + b;

printf("Sum = %d", sum);

return 0;
}

✔ Output:

Sum = 15

✔ Key Points to Remember

✔ Stored in CPU register (faster)


✔ Used for frequently accessed variables
✔ Cannot use & operator
✔ Scope is local
✔ Compiler may ignore register

You might also like