Constants and Variables in C

Test on C constants and variables C variables

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.

Variables in C Top ↑

int count = 10;
double price = 45.75;
char grade = 'A';

The variable name identifies the value so it can be read or changed later.

Variable Naming Rules Top ↑

  • Use letters, digits and underscores.
  • The first character cannot be a digit.
  • Spaces and punctuation such as - are not allowed inside an identifier.
  • C keywords such as int, return and while cannot be used as variable names.
  • C is case-sensitive, so total and Total are different identifiers.
my_var
page1
student_count

Integer, Floating and Character Constants Top ↑

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

Read-Only Objects with const Top ↑

#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.

Variables Can Change Top ↑

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.





plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer