0% found this document useful (0 votes)
4 views

Storage Classes v 1

The document explains storage classes in C programming, which define the lifetime, visibility, and scope of variables and functions. It details four primary storage classes: auto, register, static, and extern, along with their characteristics and examples. Additionally, it covers type qualifiers such as const and volatile, which control variable mutability and optimization by the compiler.

Uploaded by

ragavik430
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Storage Classes v 1

The document explains storage classes in C programming, which define the lifetime, visibility, and scope of variables and functions. It details four primary storage classes: auto, register, static, and extern, along with their characteristics and examples. Additionally, it covers type qualifiers such as const and volatile, which control variable mutability and optimization by the compiler.

Uploaded by

ragavik430
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Storage classes

• In C programming, storage classes define the lifetime,


visibility, and scope of variables or functions.
• They specify how and where a variable is stored in memory
and for how long it exists during the execution of a program.
• There are four primary storage classes in C:
1. auto
2. register
3. static
4. extern

January 23, 2025 1


Auto (default for local variables)

• Default Storage Class: For local variables (variables defined within a


function or block).

• Scope: Local to the block or function where it is defined.

• Lifetime: Only during the execution of the function or block.

• Visibility: Can be accessed only within the function or block.

• Variables declared as auto are automatically created when a function


is called and destroyed when the function exits.

January 23, 2025 2


• #include <stdio.h>
• int main()
•{
• int x,z;
• scanf("%d", &x);
• {
• int y;
• y=2;
• printf("%d",y);
• }
• z =x+y;// accessing y will result in compilation error
• printf("%d", z);
• return 0;
•}
January 23, 2025 3
Register

• Used to suggest to the compiler that a variable should be


stored(created) in a CPU register rather than RAM for faster
access.
• However, the compiler may ignore this request.
• Scope: Local to the block or function.
• Lifetime: Same as auto; exists only within the block.
• Visibility: Visible only inside the block or function.
• Note: You cannot take the address of a register variable
because it may not reside in memory.

January 23, 2025 4


• #include <stdio.h>
• int main()
•{
• register int count=0;
• //scanf("%d", &count); taking address of a variable –count
• // will result in compilation error
• count++;
• printf("\n%d", count);
• return 0;
•}

January 23, 2025 5


Static

• Static variables retain their value between function calls,


meaning their lifetime is the entire execution of the program.
• Variables declared as static inside a function will persist
across function calls.
• Scope: If declared inside a function, the variable is local to
the function but retains its value across calls. If declared
outside a function, it is global but cannot be accessed outside
the file (i.e., its visibility is limited to the file).
• Lifetime: The entire duration of the program.
• Visibility: Limited to the function or file in which it is
declared.

January 23, 2025 6


• #include <stdio.h>
• void incrementCount() {
• static int count = 0; // static variable
• count++;
• printf("Count: %d\n", count);
•}
• int main() {
• incrementCount(); // Output: Count: 1
• incrementCount(); // Output: Count: 2
• incrementCount(); // Output: Count: 3
• return 0;
•}

January 23, 2025 7


Static variable in global scope

In this example, the static keyword is used for a global variable,


limiting its visibility to the current file.

January 23, 2025 8


Static variable in global scope

• #include <stdio.h>
• static int globalVar = 10; // static variable at global scope
• void displayGlobalVar() {
• printf("Global Variable: %d\n", globalVar);
•}
• int main() {
• displayGlobalVar(); // Output: Global Variable: 10
• globalVar = 20; // Modifying static variable
• displayGlobalVar(); // Output: Global Variable: 20
• return 0;
•}
January 23, 2025 9
Extern

• The extern keyword is used to declare variables that are


defined in another file.
• It indicates that the variable or function exists, but its
memory or implementation is found in another translation
unit.
• Scope: Can be accessed globally across different files (linking
with other object files).
• Lifetime: The same as the variable it refers to, i.e., it exists for
the entire program's duration.
• Visibility: Visible to other files or functions that use the
extern declaration.

January 23, 2025 10


Extern-banking.h This is the header file that declares
the functions and the global variable, and it will be
included in other source files.
//Declaration of the
global //variable from
another file and
// Function declarations
extern int balance;
void deposit(int);
void withdraw(int);;

January 23, 2025 11


Extern-banking.c(Main program) This file will
contain the main function and call the deposit and
withdraw functions, which are declared in other files.
#include <stdio.h>
#include “banking.h”
int main()
{ // Initial deposit
deposit(5000);
printf("After deposit, balance: %d\n", balance);
// Withdraw some money
withdraw(1500);
printf("After withdrawal, balance: %d\n", balance);
// Withdraw more money than available
withdraw(4000);
printf("After attempting to withdraw more than balance,
balance: %d\n", balance);
return 0;
January 23, 2025 12
}
Extern-bank_operations.c(modify global balance)
#include <stdio.h>
int balance = 0; // Definition of the global
variable 'balance‘
void deposit(int amount)
{
balance += amount;
printf("Deposited: %d\n", amount);
}
void withdraw(int amount)
{
if (amount > balance) {
printf("Insufficient balance! Cannot
withdraw %d\n", amount);
}
else {
balance -= amount;
printf("Withdrawn: %d\n", amount);
}
}
January 23, 2025 13
Extern

•Compile:
gcc banking.c bank_operations.c -o banking_program
•Run the program:
./banking_program
Sample Output:
Deposited: 5000
After deposit, balance: 5000
Withdrawn: 1500
After withdrawal, balance: 3500
Insufficient balance! Cannot withdraw 4000
After attempting to withdraw more than balance, balance: 3500
January 23, 2025 14
Type qualifiers

The const qualifier is used to indicate that the value of a variable


cannot be changed after initialization.

It makes the variable constant, preventing


any modifications to its value.
• Usage:
• const int x = 5; — Here, x is a constant integer,
and trying to change x later will result in a compiler error.

January 23, 2025 15


Type qualifiers

• You can use const with pointers too. For example:


• const int *ptr; — ptr is a pointer to a constant integer
• (i.e., you cannot modify the integer through ptr).
• int * const ptr; — ptr is a constant pointer
• (i.e., the pointer's address cannot be changed, but the data
it points to can be modified).
• const int * const ptr; — The pointer is constant, and the data
it points to is also constant.
.

January 23, 2025 16


Type qualifiers

• The volatile keyword can be applied to variables of any type,


including int, float, double, and even struct and union types.

• When a variable is declared volatile, the compiler generates


code that reads its value from memory each time it is accessed.
It ensures that the variable's current value is always used.

January 23, 2025 17


Type qualifiers
• The volatile qualifier is used to tell the compiler that the value
of a variable can change unexpectedly, usually due to external
factors (such as hardware, interrupt service routines, or multi-
threaded applications).

• This prevents the compiler from optimizing reads or writes to


that variable.
• volatile int x; — x can be changed at any time, and the
compiler will not optimize out any access to it.

• This is commonly used in embedded systems where a variable


might be modified by hardware or another thread.

• Example: In interrupt service routines, a variable may be


updated by hardware, and volatile ensures that the compiler
doesn't optimize the access to it.
January 23, 2025 18

You might also like