Skip to content
“`html Variables in C

Variables in C

Learning Objectives: By the end of this lesson, you should be able to:
  • Understand what a variable is.
  • Declare variables in C.
  • Store values in variables.
  • Display variable values using printf().
  • Use common format specifiers.
  • Perform calculations using variables.

1. What is a Variable?

A variable is a named memory location used to store a value of a particular data type.

The value stored in a variable can change during program execution. This is why it is called a variable.

A variable is referred to using an identifier throughout the program.

Identifier
x
Memory Location
Value
25
Simple example: If we write int age = 20;, the variable age is used to refer to a memory location containing the integer value 20.

2. Creating a Variable – Declaration

A variable is created using a declaration statement.

A declaration introduces a variable and specifies the type of data that it can store.

Syntax

datatype identifier;

Example

int x;
Part Meaning
int Data type
x Identifier / variable name
; Terminates the statement

3. Basic Data Types in C

A data type specifies what kind of data a variable can store.

Data Type Used For Example Format Specifier
int Integer numbers 25 %d
float Decimal numbers 2.34 %f
double Decimal numbers with greater precision 25.6789 %f with printf()
char A single character 'A' %c
Important: In printf(), %f is normally used to print floating-point values, including values passed as double. The %lf specifier is primarily used with scanf() for reading a double.

4. Storing Data into a Variable

A value can be stored in a variable using an assignment statement. A value can also be obtained from the user using input functions such as scanf().

Assignment Operator

The symbol = is called the assignment operator in C.

Example:

x = 10;
This means: store the value 10 in the variable x.

5. General Forms of Assignment Statement

Form Example Meaning
var = val; x = 1; Assign a value to a variable.
var = var; y = x; Copy the value of one variable into another.
var = expr; z = x + y; Assign the result of an expression.
var = func(); a = test(); Assign a function’s return value.
Remember: The assignment operator = does not mean “is equal to” in the mathematical sense. It means “assign/store the value on the right into the variable on the left.”

6. Displaying the Value of a Variable

The printf() function is used to display values on the monitor.

General Syntax

printf(“%fsp”, value);
printf(“%fsp”, var);
printf(“%fsp”, expr);

Here %fsp represents an appropriate format specifier.

7. Format Specifiers

A format specifier is a special sequence beginning with % that tells printf() what type of value should be displayed.

Format Specifier Data Type Example
%d int printf("%d", x);
%f float / floating-point output printf("%f", temp);
%c char printf("%c", ch);
%s String printf("%s", name);

8. Example 1 – Integer Variables and Expressions

C Program
#include <stdio.h>

int main()
{
    printf("%d", 5);

    int x;
    x = 15;

    printf("\nx = %d", x);
    printf("\n%d", x + 5);

    return 0;
}

Output

5 x = 15 20
Explanation:
  • %d is used to display an integer.
  • x = 15; stores 15 in x.
  • x + 5 produces the value 20.
  • \n moves the cursor to the next line.

9. Example 2 – Displaying Two Variables

C Program
#include <stdio.h>

int main()
{
    int x, y;

    x = 12;
    y = 24;

    printf("x = %d, y = %d", x, y);

    return 0;
}

Output

x = 12, y = 24

10. Example 3 – Float Variable and Precision

C Program
#include <stdio.h>

int main()
{
    float temp;

    temp = 2.34;

    printf("temp = %.2f", temp);

    return 0;
}

Output

temp = 2.34
What does %.2f mean?

The .2 specifies the number of digits to display after the decimal point.

For example:

  • %.2f → 2 digits after decimal point
  • %.3f → 3 digits after decimal point
  • %.1f → 1 digit after decimal point

11. Example 4 – Character Variable

C Program
#include <stdio.h>

int main()
{
    char choice;

    choice = '#';

    printf("choice = %c", choice);

    return 0;
}

Output

choice = #
Remember: A character constant is written inside single quotes. For example: 'A', '#', and '9'.

12. Example 5 – Changing the Value of a Variable

A variable can hold different values at different times during program execution.

C Program
#include <stdio.h>

int main()
{
    char ch;

    ch = 'A';
    printf("ch = %c\n", ch);

    ch = '#';
    printf("ch = %c\n", ch);

    ch = '9';
    printf("ch = %c", ch);

    return 0;
}

Output

ch = A ch = # ch = 9
Important observation: The variable ch is the same variable throughout the program, but its stored value changes from 'A' to '#' and finally to '9'.

13. Declaration vs Assignment

Statement Purpose
int x; Declares an integer variable.
x = 25; Assigns 25 to the variable.
int x = 25; Declares and initializes the variable.
Initialization: Giving a variable its first value at the time of declaration is called initialization.

Example: int age = 18;

14. Rules for Naming Variables

  • A variable name can contain letters, digits, and underscore.
  • A variable name must not begin with a digit.
  • Spaces are not allowed in variable names.
  • C keywords cannot be used as variable names.
  • C is case-sensitive.
Valid Invalid
age 2age
studentName student name
total_marks total-marks
number1 int

15. Practice Questions

Basic Practice

1. Write a C program to create an integer variable with the identifier first, store the value 25 in it, and display it on the monitor.
2. Write a C program to create two integer variables, assign your desired values to them, and display their sum.
3. Write a C program to calculate and display the area of a rectangle for a given length and breadth.

Formula: Area = length × breadth
4. Write a C program to calculate and display the area of a circle for a given radius.

Formula: Area = π × radius × radius
5. Write a C program to calculate and display the average of any three numbers.

Formula: Average = (Number1 + Number2 + Number3) / 3

Additional Practice

6. Create a float variable called price, assign a value to it, and display it with exactly two digits after the decimal point.
7. Create a character variable called grade, store the character 'A', and display it.
8. Create two integer variables and display their sum, difference, product, and quotient.
9. Write a program to calculate the perimeter of a rectangle.

Formula: Perimeter = 2 × (length + breadth)
10. Write a program to convert a temperature from Celsius to Fahrenheit.

Formula: Fahrenheit = (Celsius × 9 / 5) + 32

16. Think and Answer

1. What is the difference between a variable and an identifier?
2. What is the purpose of a data type?
3. What does the assignment statement x = 10; do?
4. What is the difference between int x; and int x = 10;?
5. What is the purpose of %d in printf()?
6. What does %.2f mean?
7. Why is 'A' written using single quotes?

17. Quick Revision

Declare
int x;
Assign
x = 25;
Process
x + 5
Display
printf()
Remember:
  • A variable is a named memory location used to store data.
  • A declaration specifies the variable’s data type and name.
  • The = operator is used for assignment.
  • printf() is used to display values.
  • Format specifiers tell printf() what type of value is being displayed.
  • A variable’s value can change during program execution.
C Programming – Variables
Learn → Declare → Assign → Process → Display → Practice
“`