Introduction

In C programming, variables are like containers that store data. They allow programmers to manipulate and store values such as numbers, characters, and decimals during program execution. Understanding variables is one of the first and most important steps to becoming a successful C programmer.

Variables have different types depending on the kind of data they store. The most commonly used types in C are:

  • int: Stores whole numbers, e.g., 1, 25, -10
  • float: Stores decimal numbers, e.g., 3.14, 7.5
  • char: Stores a single character, e.g., 'A', 'b', '9'

In this tutorial, we will explain each type in detail with simple programs, common mistakes, practice exercises, and multiple-choice questions (MCQs) for effective learning.

Single Variable Programs

Single variable programs are the simplest C programs and are a great way to understand variable declaration, assignment, and printing values.

1. Program Using int

#include <stdio.h>
int main() {
    int age = 22;
    printf("My age is %d\n", age);
    return 0;
}

Explanation: Here, age is an integer variable. We use %d in printf to print its value. This program demonstrates how to declare, assign, and print an integer variable.

Output: My age is 22

Common Mistake: Forgetting to use %d or using quotes incorrectly will cause errors.

2. Program Using float

#include <stdio.h>
int main() {
    float height = 5.9;
    printf("My height is %.1f feet\n", height);
    return 0;
}

Explanation: The variable height stores decimal numbers. The %.1f specifier ensures the number is printed with 1 decimal point. Float variables are used when precise decimal values are needed.

Output: My height is 5.9 feet

3. Program Using char

#include <stdio.h>
int main() {
    char grade = 'A';
    printf("My grade is %c\n", grade);
    return 0;
}

Explanation: Char stores a single character. The %c specifier prints the character. Characters must be enclosed in single quotes (' ').

Output: My grade is A

Programs with Two Variables

Using two variables allows programs to handle multiple pieces of information. Let’s see some examples combining different types:

1. int and float

#include <stdio.h>
int main() {
    int rollNo = 101;
    float marks = 87.5;
    printf("Roll No: %d\n", rollNo);
    printf("Marks: %.1f\n", marks);
    return 0;
}

This program demonstrates storing both integer and decimal data for a student. It shows how to print multiple variables using separate printf statements.

Output:
Roll No: 101
Marks: 87.5

2. int and char

#include <stdio.h>
int main() {
    int age = 20;
    char grade = 'B';
    printf("Age: %d\n", age);
    printf("Grade: %c\n", grade);
    return 0;
}

This program stores an integer and a character together. Such combinations are useful in real-life applications like student records or employee data.

Output:
Age: 20
Grade: B

Programs with Three Variables

We can also declare multiple variables of different types in one program to handle complex data.

int, float, and char

#include <stdio.h>
int main() {
    int id = 123;
    float salary = 55000.75;
    char grade = 'A';
    
    printf("Employee ID: %d\n", id);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);
    return 0;
}

Explanation: This program demonstrates storing an employee’s ID, salary, and performance grade. It highlights how variables of different types can coexist in the same program.

Output:
Employee ID: 123
Salary: 55000.75
Grade: A

Programs Using Multiple Same-Type Variables

Sometimes you need multiple variables of the same type. C allows declaring them in a single line.

1. Multiple int variables

#include <stdio.h>
int main() {
    int x = 10, y = 20, z = 30;
    printf("x = %d, y = %d, z = %d\n", x, y, z);
    return 0;
}

Output: x = 10, y = 20, z = 30

2. Multiple float variables

#include <stdio.h>
int main() {
    float a = 2.5, b = 4.8, c = 6.3;
    printf("a = %.1f, b = %.1f, c = %.1f\n", a, b, c);
    return 0;
}

Output: a = 2.5, b = 4.8, c = 6.3

3. Multiple char variables

#include <stdio.h>
int main() {
    char ch1 = 'A', ch2 = 'B', ch3 = 'C';
    printf("Characters: %c, %c, %c\n", ch1, ch2, ch3);
    return 0;
}

Output: Characters: A, B, C

Common Mistakes with Variables

  • Using wrong format specifier: e.g., printing int with %f
  • Not initializing variables before using them
  • Using double quotes for char variables
  • Using undeclared variables

Practice Exercises

  • Declare 3 integers a, b, c and print in one line.
  • Declare 2 floats price and discount, print with 1 decimal.
  • Declare 3 chars ch1, ch2, ch3 and print in one line.
  • Declare an int, float, and char and print all three in one printf.
  • Try incorrect declarations to see compiler errors and learn debugging.

MCQs on Variables and Data Types

  1. Correct format specifier for int in C? a
  2. Correct format specifier for int in C?
    a) %d
    b) %f
    c) %c
    d) %s
    Answer: a) %d. The %d specifier is used for printing integer values in C.
  3. What is the size of int in most 32-bit systems?
    a) 1 byte
    b) 2 bytes
    c) 4 bytes
    d) 8 bytes
    Answer: c) 4 bytes. This can vary based on the compiler and system architecture.
  4. Which data type stores decimal values?
    a) int
    b) char
    c) float
    d) None of the above
    Answer: c) float. Use float for decimal numbers.
  5. Correct printf format for float?
    a) %f
    b) %d
    c) %c
    d) %lf
    Answer: a) %f. Use %.2f or %.1f to control decimal precision.
  6. Best data type for a single character?
    a) int
    b) char
    c) float
    d) double
    Answer: b) char. Characters are stored using single quotes, e.g., 'A'.
  7. What is the output of the program:
    #include <stdio.h>
    int main() {
        char ch = 'X';
        printf("%c", ch);
        return 0;
    }
    a) X
    b) x
    c) 88
    d) Error
    Answer: a) X. The program prints the character stored in ch.
  8. Which variables can represent negative numbers?
    a) int
    b) float
    c) char
    d) All of the above
    Answer: d) All of the above. Use signed int, float, or signed char to store negative numbers.
  9. Invalid variable declaration in C?
    a) int age = 20;
    b) float price = 99.99;
    c) char name = "Ali";
    d) char grade = 'A';
    Answer: c) char name = "Ali"; (char stores only one character).

Related Articles

Final Thoughts

Learning C programming requires patience, practice, and a clear understanding of variables and their types. Variables are the foundation of all programming logic, allowing you to store, manipulate, and display data in your programs. Starting with simple programs and gradually increasing complexity is the best approach for beginners.

Try all the practice exercises in this guide. Understand the common mistakes and why they occur. Review the MCQs to test your knowledge, and don’t hesitate to revisit previous tutorials on data types, operators, and loops for better clarity. Mastering variables is the first step toward becoming a confident C programmer.