- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Variables
![]() Share with a Friend |
C Programming - C Variables
A variable in C is a named storage location in memory used to hold data. Variables play a fundamental role in programming by allowing data manipulation and storage for calculations, logic, and data processing.
Key Points about Variables
- Declaration: Variables must be declared before use.
- Naming Rules:
- Can contain letters, digits, and underscores (_).
- Must begin with a letter or underscore.
- Cannot be a keyword.
- Case-sensitive (age and Age are different).
- Data Type: Every variable must have a type (int, float, etc.).
Syntax of Variable Declaration
datatype variable_name;
datatype variable1, variable2, ... , variablen;
Examples
int age; // Declares an integer variable
float salary; // Declares a floating-point variable
char grade; // Declares a character variable
int x = 10, y = 20; // Declares and initializes multiple variables
Variable Initialization
Variables can be initialized at the time of declaration.
Syntax
datatype variable_name = value;
Examples
int num = 5;
float pi = 3.14;
char letter = 'A';
Types of Variables in C
- Local Variables:
- Declared inside a function or block.
- Accessible only within the block where they are declared.
Example:
void display() {
int num = 10; // Local variable
printf("%d\n", num);
}
- Global Variables:
- Declared outside all functions.
- Accessible by all functions in the program.
Example:
int count = 0; // Global variable
void increment() {
count++;
}
- Static Variables:
- Retain their value between function calls.
- Default value is 0.
Example:
void demo() {
static int x = 0; // Static variable
x++;
printf("%d\n", x);
}
- Automatic Variables:
- Default for local variables.
- Keyword: auto (optional).
Example:
void display() {
auto int num = 5; // Automatic variable
printf("%d\n", num);
}
- External Variables:
- Declared using extern and defined elsewhere.
- Used to share variables across multiple files.
Example:
extern int globalVar;
Scope of Variables
- Block Scope:
- Variable accessible only within the block {} where it is defined.
- Function Scope:
- Variables declared inside a function are accessible only within the function.
- File Scope:
- Global variables are accessible throughout the file.
- Program Scope:
- extern variables accessible across multiple files.
Lifetime of Variables
- Automatic:
- Exist only during the execution of the block where they are declared.
- Static:
- Retain their value between function calls.
- Dynamic:
- Allocated and deallocated explicitly using malloc/free.
Example: Variables in Action
#include <stdio.h>
// Global variable
int globalVar = 10;
void demo() {
// Local variable
int localVar = 20;
// Static variable
static int staticVar = 0;
staticVar++;
printf("Local: %d, Static: %d, Global: %d\n", localVar, staticVar, globalVar);
}
int main() {
demo();
demo();
return 0;
}
Output
Local: 20, Static: 1, Global: 10
Local: 20, Static: 2, Global: 10
Summary
| Type | Scope | Lifetime | Keyword |
|---|---|---|---|
|
Local |
Block |
Within block execution |
None |
|
Global |
File |
Entire program execution |
None |
|
Static |
Block/File |
Entire program execution |
static |
|
External |
File/Program |
Entire program execution |
extern |
|
Automatic |
Block |
Within block execution |
auto |
