Master C Variables, Constants, and Scope: From #define to static
This article explains C variables and constants, their three components, key differences, how to define symbolic constants with #define, use the const keyword, understand various scopes, the static storage class, and the distinction between declarations and definitions, all illustrated with code examples.
Variables and Constants
Variables and constants have three elements: Name – the entry address of the memory space; Data type – the size of the memory space; Data value – the value stored in that memory.
Name : entry address of memory space.
Data type : size of memory space.
Data value : the stored value.
Two main differences:
Mutability : a variable’s value can change, a constant’s cannot.
Type variety : variables support many constructed types, while constants have relatively fixed types.
#define Symbolic Constant
Use the #define preprocessor directive to define a constant. Syntax:
#define SYMBOLIC_CONSTANT_NAME CONSTANT_EXPRESSION; #definedoes not require a data type; the compiler replaces the symbol with the value during compilation.
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '
'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}const Keyword
Constant Modifier
C provides const and #define macro for defining constants. Differences:
Type checking : const constants have a type and can be type‑checked; #define macros have no type and are simple text substitution.
Memory saving : const constants occupy a single memory location, while each macro use may generate multiple copies.
Efficiency : const constants are kept in the symbol table and do not require storage access at runtime.
Syntax:
const TYPE ConstantName = value;
extern const int ConstantName = value;Example:
#define Pi1 3.14159 // macro constant
double I = Pi1; // macro replacement, memory allocated
double J = Pi1; // another replacement, another allocation
const double Pi2 = 3.14159; // single allocation
double i = Pi2; // uses same memory
double j = Pi2; // no additional allocationConst Function Parameters
Using const on function parameters prevents modification of the argument inside the function, improving robustness and avoiding unnecessary copies.
Prevents accidental modification of parameter values.
Avoids copying, increasing efficiency.
void func(const int i)
{
i = 10; // compiler error
}Scope
In C, scope defines where a constant, variable, or function can be accessed, controlling visibility, avoiding naming conflicts, and improving safety and maintainability.
Two major categories:
Local scope
Block scope : variables defined inside a code block, destroyed after the block ends.
Function scope : variables defined inside a function body, destroyed after the function returns.
Function prototype scope : parameters declared in a function prototype, visible only within the prototype and its calls.
Global scope
File scope : constants, variables, or functions defined outside any function in a source file; accessible throughout that file but not from other files.
Variables can be classified as:
Local variables : declared inside blocks or functions.
Global variables : declared outside functions, have global scope; usually placed at the top of the source file.
static Keyword
The static keyword modifies the storage duration and visibility of variables or functions. Objects marked static are allocated in static storage before main() runs.
static local variable : scope limited to the function, but lifetime persists across calls.
static global variable : file‑level scope only; other files cannot access it, preventing name clashes.
static function : function visible only within its defining file.
Declaration and Definition
In C, a declaration introduces a name and its type to the compiler without allocating storage; a definition allocates storage and may initialize the object.
Declaration : e.g.,
extern int i; extern int i;Definition : e.g., int i; or
int i = 0; int i;
int i = 0;For functions, a declaration provides the prototype, while a definition includes the body.
int add(int a, int b); // declaration
int add(int a, int b) { return a + b; } // definitionextern Keyword
The extern keyword declares a variable or function defined in another file, enabling modular programming and code reuse.
Example files:
#include <stdio.h>
extern int x;
extern int y;
int addtwonum() { return x + y; } #include <stdio.h>
int x = 1;
int y = 1;
int addtwonum();
int main() {
int result = addtwonum();
printf("result : %d", result);
return 0;
}Compile and run:
$ gcc -Wall main.c addtwonum.c -o main
$ ./main
result : 2Storage of Constants and Variables
Constants : stored in the Data Segment as literals.
Global variables :
Initialized: Data Segment.
Uninitialized: BSS Segment.
Local variables : stored in Stack Segment.
Static variables (global or local) :
Initialized: Data Segment.
Uninitialized: BSS Segment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
