C Variables Constants #define, const, scope is an important C Language topic because it appears in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.
For this page, focus on what problem C Variables Constants #define, const, scope solves, where developers usually make mistakes, and how to verify the result. The audit note for this lesson was: under 650 content words; limited checklist/practice/mistake/FAQ notes .
A strong understanding of C Variables Constants #define, const, scope should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work with tools or output.
C Variables Constants #define const scope should be studied as a practical C Language lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the c-language > variables-constants page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
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';
| 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) |
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
*/
A constant is a value that cannot be changed after it is defined. C provides two ways to define constants:
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.
#define PI 3.14159
#define MAX_SIZE 100
#define GREETING "Hello, World!"
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
*/
When studying C Variables Constants #define, const, scope, separate three things: the concept, the syntax, and the situation where it is useful. This prevents the lesson from becoming a list of commands with no practical meaning.
In C Language, C Variables Constants #define, const, scope becomes easier when you build a tiny example first, then increase complexity. Add one realistic input, one invalid or boundary input, and one explanation of why the result changes.
#include <stdio.h>
int main(void) {
printf("C Variables Constants #define const scope: normal path\n");
return 0;
}
#include <stdio.h>
int main(void) {
int count = 0;
if (count == 0) printf("C Variables Constants #define const scope: empty input\n");
return 0;
}
Memorizing C Variables Constants #define const scope without the situation where it is useful.
Connect C Variables Constants #define const scope to a concrete C Language task.
Testing C Variables Constants #define const scope only with the perfect input.
Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Changing code before reading the visible symptom or error message.
Inspect the output, state, configuration, or stack trace connected to C Variables Constants #define const scope.
Memorizing C Variables Constants #define const scope without the situation where it is useful.
Connect C Variables Constants #define const scope to a concrete C Language task.
The common mistake is memorizing syntax without understanding when the behavior changes or fails.
Remember the problem it solves in C Language, then attach the syntax or steps to that problem.
You can predict the result of a small example, explain a failure case, and choose it over a nearby alternative for a clear reason.
They often copy the syntax but skip the state, input, dependency, selector, route, type, or configuration that controls the behavior.
Explore 500+ free tutorials across 20+ languages and frameworks.