C Variables and Constants
Variables
A variable is a named memory location that stores a value. In C, you must declare a variable before using it by specifying its data type and name.
// Syntax
type variableName;
type variableName = value; // declaration with initialization
// Examples
int age;
int age = 25;
float salary = 50000.50;
char initial = 'J';
Variable Naming Rules
- Must start with a letter (a“z, A“Z) or underscore
_ - Can contain letters, digits (0“9), and underscores
- Cannot contain spaces or special characters (
@,#,$, etc.) - Cannot be a C keyword (e.g.,
int,return,if) - Case-sensitive:
ageandAgeare different variables - No length limit (but only first 31 characters are significant in older standards)
| Valid Names | Invalid Names |
|---|---|
age, _count, total_price, num1 | 1num (starts with digit) |
firstName, MAX_SIZE | my-var (contains hyphen) |
x, y, z | int (reserved keyword) |
Variable Scope
Local variables are declared inside a function and can only be used within that function. Global variables are declared outside all functions and can be accessed from anywhere in the program.
#include <stdio.h>
int globalVar = 100; // global variable — accessible everywhere
void display() {
int localVar = 50; // local variable — only inside display()
printf("Inside display() - localVar: %d\n", localVar);
printf("Inside display() - globalVar: %d\n", globalVar);
}
int main() {
int localVar = 10; // different localVar — local to main()
printf("Inside main() - localVar: %d\n", localVar);
printf("Inside main() - globalVar: %d\n", globalVar);
globalVar = 200; // modify global variable
display();
// printf("%d", localVar from display()); // ERROR — not accessible here
return 0;
}
/*
Output:
Inside main() - localVar: 10
Inside main() - globalVar: 100
Inside display() - localVar: 50
Inside display() - globalVar: 200
*/
Constants
A constant is a value that cannot be changed after it is defined. C provides two ways to define constants:
#define Macro (Preprocessor Constant)
The #define directive creates a macro — the preprocessor replaces every occurrence of the name with the value before compilation. No memory is allocated.
#define PI 3.14159
#define MAX_SIZE 100
#define GREETING "Hello, World!"
const Keyword
The const keyword declares a variable whose value cannot be modified. Unlike #define, it has a type and occupies memory.
const float PI = 3.14159f;
const int MAX = 100;
#include <stdio.h>
#define PI 3.14159 // preprocessor macro — no type, no semicolon
#define MAX_SCORE 100
#define AUTHOR "Dennis Ritchie"
int main() {
// const variable — has type, stored in memory
const int MIN_AGE = 18;
const float GRAVITY = 9.81f;
float radius = 5.0;
float area = PI * radius * radius;
printf("Area of circle: %.2f\n", area);
printf("Max score: %d\n", MAX_SCORE);
printf("Author: %s\n", AUTHOR);
printf("Min age: %d\n", MIN_AGE);
printf("Gravity: %.2f m/s^2\n", GRAVITY);
// MIN_AGE = 21; // ERROR: assignment of read-only variable
return 0;
}
/*
Output:
Area of circle: 78.54
Max score: 100
Author: Dennis Ritchie
Min age: 18
Gravity: 9.81 m/s^2
*/
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.