Lesson 9 of 60 – Variables in C
15%

Variables in C

A variable is a named memory location used to store data in a C program. The value stored in a variable can be used in calculations, conditions, functions, and other operations.

Note: Every variable in C has a data type and a name. The data type determines what kind of value the variable can store.

1. What is a Variable?

A variable is a named storage location that holds a value. For example:

int age = 20;

Here, age is a variable that stores the value 20.

2. Why Do We Use Variables?

Variables allow us to:

  • Store information.
  • Use stored values later.
  • Perform calculations.
  • Change values during program execution.
  • Make programs easier to understand.

3. Variable Declaration

A variable can be declared by specifying its data type followed by its name.

int age;

This declares an integer variable named age.

4. Variable Initialization

Initialization means assigning an initial value to a variable when it is declared.

int age = 20;

Here, age is initialized with the value 20.

5. Declaration and Assignment Separately

A variable can first be declared and then assigned a value.

int age;

age = 20;

Both declaration and assignment are separate statements in this example.

6. Variable Data Type

The data type tells the compiler what kind of value a variable is intended to store.

int age = 20;
float price = 99.50;
char grade = 'A';

Different data types are suitable for different kinds of values.

7. Integer Variable

The int type is commonly used for integer values.

int age = 25;
int marks = 85;

These variables store whole-number values.

8. Float Variable

The float type can store floating-point values.

float price = 99.50f;

The f suffix can be used to indicate a float literal.

9. Character Variable

The char type is used to store a single character.

char grade = 'A';

A character constant is normally written inside single quotation marks.

10. Double Variable

The double type is used for floating-point values with greater precision than float.

double salary = 25000.75;

11. Rules for Variable Names

Common rules for variable names include:

  • Use letters, digits, and underscores.
  • The name must not start with a digit.
  • Do not use a C keyword as a variable name.
  • C variable names are case-sensitive.

12. Valid Variable Names

Examples of valid variable names:

age
studentName
marks1
_total
course_fee

13. Invalid Variable Names

Examples of invalid variable names:

1age
student-name
total fee
int

These names violate one or more C identifier rules.

14. Variables are Case-Sensitive

C treats uppercase and lowercase letters as different.

int age = 20;
int Age = 30;

Here, age and Age are two different variables.

15. Changing a Variable Value

A variable's value can be changed after declaration.

int age = 20;

age = 25;

After the assignment, age contains 25.

16. Using a Variable in printf()

Variables can be displayed using printf().

#include <stdio.h>

int main()
{
    int age = 20;

    printf("%d", age);

    return 0;
}

17. Integer Format Specifier

The %d conversion specifier is commonly used with an int value in printf().

int marks = 85;

printf("%d", marks);

18. Float Format Specifier

A floating-point value can be printed with %f.

float price = 99.50f;

printf("%f", price);

19. Character Format Specifier

The %c conversion specifier is used to print a character.

char grade = 'A';

printf("%c", grade);

20. Multiple Variables

Multiple variables can be declared in a program.

int age = 20;
float height = 5.8f;
char grade = 'A';

Each variable can store a different type of value.

21. Multiple Variables of Same Type

Variables of the same data type can be declared together.

int a = 10, b = 20, c = 30;

This creates three integer variables.

22. Variables in Calculations

Variables are frequently used in mathematical calculations.

int a = 10;
int b = 20;

int sum = a + b;

printf("%d", sum);

The value of sum becomes 30.

23. Variable Assignment

The assignment operator = is used to assign a value to a variable.

int marks;

marks = 90;

The value on the right side is assigned to the variable on the left side.

24. Variable Assignment from Another Variable

One variable can receive the value of another variable.

int a = 10;
int b;

b = a;

After the assignment, b contains the value 10.

25. Local Variables

A variable declared inside a function or block is generally a local variable. Its scope is limited to the block where it is declared.

int main()
{
    int age = 20;

    printf("%d", age);

    return 0;
}

26. Global Variables

A variable declared outside all functions has file scope and is commonly called a global variable.

#include <stdio.h>

int count = 10;

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

    return 0;
}

27. Constants vs Variables

Variable Constant
Value can generally be changed Value is intended not to be changed through the declared constant
int age = 20; const int age = 20;

28. Good Variable Names

Use meaningful names that describe the stored value.

int studentAge;
float courseFee;
int totalMarks;

Meaningful names make programs easier to read.

29. Complete Variable Example

#include <stdio.h>

int main()
{
    int age = 20;
    int marks = 85;
    float fee = 5000.0f;
    char grade = 'A';

    printf("Age = %d\n", age);
    printf("Marks = %d\n", marks);
    printf("Fee = %.2f\n", fee);
    printf("Grade = %c\n", grade);

    return 0;
}

30. Practice Program

Create a program that stores and displays a student's name, age, marks, and grade.

#include <stdio.h>

int main()
{
    int age = 18;
    int marks = 90;
    char grade = 'A';

    printf("Age = %d\n", age);
    printf("Marks = %d\n", marks);
    printf("Grade = %c\n", grade);

    return 0;
}

Practice by changing the variable values and observing the output.

📌 Key Points

  • A variable is a named storage location for a value.
  • Every variable has a data type and a name.
  • Variables can be declared and initialized.
  • The assignment operator = assigns a value to a variable.
  • C variable names are case-sensitive.
  • Variable names must follow C identifier rules.
  • int, float, double, and char are common C data types.
  • Variables can be used in calculations and output statements.
  • Local variables have limited scope, while variables declared outside functions can have file scope.

🧠 Quick Quiz

Question: Which symbol is used to assign a value to a variable in C?