Variables in C

A variable is a named memory location used to store data. Its value can change while the program is running.

Basic syntax

data_type variable_name = value;

 

Examples

int age = 21;
float salary = 45000.50f;
char grade = ‘A’;

 

Three important words

Declaration – Telling C the variable’s type and name.

Initialization – Giving a variable its first value when it is created.

Assignment – Giving or changing a value after declaration.

int marks;       // declaration
marks = 85;      // assignment

int age = 20;    // declaration + initialization

 

Rules for variable names

  • May contain letters, digits and underscore (_).
  • Must not begin with a digit.
  • Spaces are not allowed.
  • A keyword cannot be used as a variable name.
  • C is case-sensitive.

Program using variables

#include <stdio.h>

int main() {
int quantity = 5;
float price = 120.50f;
float total = quantity * price;

printf(“Quantity = %d\n”, quantity);
printf(“Price = %.2f\n”, price);
printf(“Total = %.2f\n”, total);

return 0;
}

 

OUTPUT
Quantity = 5
Price = 120.50
Total = 602.50

 

Scroll to Top