keyword-static
title: "courses/c-course - keyword-static.md"
- **fileName**: keyword-static
- **Created on**: 2024-06-13 10:41:46
static keyword have to major case to use:
- Static Variables in Functions (Local Scope): When a variable is declared with
static
inside a function, its lifetime extends across the entire run of the program. Unlike normal local variables, which are created and destroyed each time a function is called and returns, static local variables are initialized only once and retain their value between function call . - Static Variables in Global Scope (File Scope): When a global variable is declared with
static
, its scope is limited to the file in which it is declared. This prevents the variable from being accessed or modified by other files, thus providing a way to encapsulate data within a file. - When a function is declared with
static
, its visibility is limited to the file in which it is declared. This is similar to static global variables, preventing the function from being called from other files.
there is two way for store the value for variable:
- Global Variables: Stored in the data segment, lifetime of the program.
- Static Global Variables: Stored in the data segment, file scope, lifetime of the program.
- Dynamic Memory Allocation: Stored in the heap, persists until freed.
- Register Variables: Stored in CPU registers (if possible), limited to function call duration.
- Global Variables : Global variables are declared outside of any function and are accessible from any function within the same file (and other files if declared with
extern
).
- Storage Duration: They persist for the lifetime of the program.
- Storage Location: Typically stored in the data segment of the program's memory.
- Initialization: Automatically initialized to zero if not explicitly initialized.
#include <stdio.h>
int globalVar = 0; // Global variable
void increment() {
globalVar++;
printf("globalVar = %d\n", globalVar);
}
int main() {
increment(); // Outputs: globalVar = 1
increment(); // Outputs: globalVar = 2
return 0;
}
- Static Global Variables: Static global variables have the same characteristics as global variables but with file scope, meaning they are not accessible from other files.
- Storage Duration: Lifetime of the program.
- Storage Location: Data segment.
- Initialization: Automatically initialized to zero if not explicitly initialized.
#include <stdio.h>
static int staticGlobalVar = 0; // Static global variable
void increment() {
staticGlobalVar++;
printf("staticGlobalVar = %d\n", staticGlobalVar);
}
int main() {
increment(); // Outputs: staticGlobalVar = 1
increment(); // Outputs: staticGlobalVar = 2
return 0;
}
- Dynamic Memory Allocation: Dynamic memory allocation allows you to allocate memory at runtime using functions from the C standard library, such as
malloc
,calloc
,realloc
, andfree
.
- Storage Duration: Until explicitly deallocated usingfree
.
- Storage Location: Heap.
- Initialization: Uninitialized (garbage value) if usingmalloc
or explicitly initialized to zero if usingcalloc
.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *dynamicVar = (int *)malloc(sizeof(int)); // Dynamic allocation
if (dynamicVar == NULL) {
printf("Memory allocation failed\n");
return 1;
}
*dynamicVar = 5;
printf("dynamicVar = %d\n", *dynamicVar);
free(dynamicVar); // Free allocated memory
return 0;
}
- Register Variables : Register variables are stored in the CPU registers instead of RAM (though the compiler ultimately decides whether to honor the request).
- Storage Duration: Duration of the function call.
- Storage Location: CPU registers (if available).
- Initialization: Uninitialized (garbage value).
#include <stdio.h>
void func() {
register int regVar = 0; // Register variable
regVar++;
printf("regVar = %d\n", regVar);
}
int main() {
func();// Outputs: regVar = 1 return 0;
}
The stack is used to store:
- Local variables: Declared within functions and existing only for the duration of the function call.
- Function parameters: Passed to functions and used within their scope.
- Return addresses: Allow the program to return to the correct point after a function call.
- Function call context: The entire state of a function call, including local variables, parameters, and return addresses.
#include <stdio.h>
// Function declarations
void funcB(int x);
void funcA(int y);
void funcB(int x) {
int b = x + 10; // Local variable b, function parameter x
printf("In funcB: x = %d, b = %d\n", x, b);
}
void funcA(int y) {
int a = y + 5; // Local variable a, function parameter y
printf("In funcA: y = %d, a = %d\n", y, a);
funcB(a); // Call funcB with a as parameter
}
int main() {
int mainVar = 2; // Local variable in main
printf("In main: mainVar = %d\n", mainVar);
funcA(mainVar); // Call funcA with mainVar as parameter
return 0;
}
continue:stdlib.md
before:pointer-memoryAdress