A variable is a named object whose stored value can change while a program runs. A constant represents a value that should not be modified by the program after it is defined.
int count = 10;
double price = 45.75;
char grade = 'A';
The variable name identifies the value so it can be read or changed later.
- are not allowed inside an identifier.int, return and while cannot be used as variable names.total and Total are different identifiers.my_var
page1
student_count
Constants written directly in source code are often called literals.
345 // integer constant
-543 // integer constant
125.34 // floating constant
3.2e2 // exponential floating constant
'B' // character constant
"Welcome" // string literal
#include <stdio.h>
int main(void) {
const int max_users = 45;
const char grade = 'A';
const char message[] = "Welcome";
printf("Maximum users: %d\n", max_users);
printf("Grade: %c\n", grade);
printf("Message: %s\n", message);
return 0;
}
Output
Maximum users: 45
Grade: A
Message: Welcome
The const qualifier tells the compiler that your program should not modify that object through its declared name.
int score = 45;
printf("%d\n", score);
score = 50;
printf("%d\n", score);
Output
45
50
Next, learn how the location of a declaration affects visibility and lifetime in local, global and static variables.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.