SrcForge

Lesson 6

Variables and Constants

Variables store values that can change during program execution, while constants store fixed values that remain unchanged. Understanding both is fundamental to writing effective C programs.

C Track

Save progress as you learn.

44 lessons

Lesson content

Variables and Constants in C Programming: Declaration, Types, and Examples

In every C program, variables and constants are used to store data. A variable is a named memory location whose value can change during program execution, while a constant is a fixed value that cannot be modified once it has been defined. Understanding variables and constants is one of the most important concepts in C programming because almost every program uses them to store and manipulate data.

What Are Variables and Constants in C?

  • Variable: A named memory location used to store data whose value can change during program execution.
  • Constant: A fixed value that remains unchanged throughout the execution of a program.
  • const: A keyword used to declare a constant variable whose value cannot be changed after initialization.
  • Variable Declaration: Specifying a variable's data type and name before using it.
  • Variable Initialization: Assigning a value to a variable at the time of declaration.

Variables in C Programming

A variable is a named memory location used to store data. The value stored in a variable can change while the program is running. Every variable has three important attributes: a name (identifier used to access it), a value (data currently stored), and an address (memory location where it is stored).

Rules for Naming Variables

  • A variable name must begin with a letter or an underscore (_).
  • It cannot begin with a digit.
  • Variable names are case-sensitive.
  • Only the underscore (_) is allowed as a special character.
  • Spaces are not allowed.
  • Keywords cannot be used as variable names.
  • Use meaningful and descriptive names.
Valid Variable Names
  • sum
  • count
  • area
  • roll_no
  • _total
Invalid Variable Names
  • roll no - Contains a space
  • %marks - Contains a special character
  • int - Reserved keyword
  • 2value - Starts with a digit

Variable Declaration

A variable must be declared before it is used.

Syntax
data_type variable_name;
Example
int age;
float salary;
char grade;

int length, breadth, area;

Variable Initialization

Assigning a value to a variable when it is declared is called initialization.

Syntax
data_type variable_name = value;
Example
int age = 20;
float price = 199.99;
char grade = 'A';

Variable Assignment

Values can also be assigned after declaration using the assignment operator (=).

Syntax
variable_name = value;
Example
age = 25;
price = 250.50;

Constants in C Programming

A constant is a value that cannot be changed during the execution of a program. Constants make programs easier to understand and prevent accidental modification of important values. Common examples include the value of PI, number of days in a week, maximum marks, or speed of light.

Types of Constants in C

Integer Constants

Integer constants represent whole numbers without decimal points.

10
250
-75
0
Floating-Point Constants

Floating-point constants represent numbers containing decimal values.

3.14
25.75
-10.5
Character Constants

Character constants store a single character enclosed within single quotes.

'A'
'Z'
'9'
'@'
String Constants

A string constant is a sequence of characters enclosed within double quotation marks.

"Hello"
"C Programming"
Enumeration Constants

Enumeration constants are named integer constants defined using the enum keyword.

enum Day {MON, TUE, WED};
  • Enumeration Constant: A named integer constant defined as part of an enum type.

Constant Variables (const)

A constant variable is declared using the const keyword. Once initialized, its value cannot be modified.

Syntax

const data_type variable_name = value;

Example

const float PI = 3.14159;
const int MAX_STUDENTS = 50;

Advantages of Constant Variables

  • Prevents accidental changes
  • Improves program safety
  • Makes code easier to understand

Example: Variables and Constants

#include <stdio.h>                          // Includes standard input/output functions

int main()                                  // Main function
{
    int age = 20;                           // Variable declaration and initialization

    float salary = 35000.50;                // Variable declaration and initialization

    const float PI = 3.14159;               // Constant variable

    printf("Age: %d\n", age);               // Displays the value of age

    printf("Salary: %.2f\n", salary);       // Displays the value of salary

    printf("PI: %.5f\n", PI);               // Displays the constant PI

    age = 21;                               // Variable value can be changed

    // PI = 3.14;                           // Invalid: Constant values cannot be modified

    printf("Updated Age: %d\n", age);       // Displays updated age

    return 0;                               // Ends the program
}

Variable vs Constant

  • Value: Variable can change during execution. Constant cannot change after definition.
  • Modification: Variable modification is allowed. Constant modification is not allowed.
  • Declaration: Variable uses a data type (e.g., int age = 20;). Constant uses the const keyword (e.g., const int MAX = 100;).

Best Practices

  • Use meaningful variable names.
  • Initialize variables before using them.
  • Choose the correct data type.
  • Declare constants using the const keyword whenever values should not change.
  • Follow consistent naming conventions.
  • Use uppercase names for constants (e.g., MAX_SIZE, PI) to improve readability.

When NOT to Use Constants

Avoid using constants when the value is expected to change during program execution, when user input determines the value, or when the value depends on calculations performed at runtime. In such cases, use variables instead.

FAQ About Variables and Constants in C

1. What is a variable in C programming?

A variable is a named memory location used to store data whose value can change during program execution.

2. What is a constant in C programming?

A constant is a fixed value that remains unchanged throughout the execution of a program.

3. What is the difference between a variable and a constant?

A variable's value can be modified during execution, whereas a constant's value cannot be changed once it has been defined.

4. Why do we use the const keyword in C?

The const keyword prevents a variable's value from being modified after initialization, making programs safer and easier to maintain.

5. Can a constant occupy memory?

Yes. A constant declared using the const keyword occupies memory just like a regular variable, but its value cannot be changed after initialization.

6. What are the rules for naming variables in C?

Variable names must start with a letter or underscore, cannot contain spaces or special characters (except _), cannot be C keywords, and are case-sensitive.

Prev: Data TypesBack to hub