Back to C Programming
2026-07-125 min read

variable (C Programming)

Learn variable (C Programming) step by step with clear examples and exercises.

Why This Matters

Understanding variables is crucial for mastering C programming. They allow you to store and manipulate data during program execution, making them essential for solving complex problems and creating practical applications. Knowledge of variables can help you excel in coding interviews, debug real-world bugs, and create efficient code that performs well under pressure.

Prerequisites

Before diving into C variables, it's important to have a solid foundation in the C programming language. Familiarize yourself with topics such as:

  1. Basic syntax: variables, keywords, operators, and statements
  2. Data types: int, char, float, and double
  3. Control structures: if, else, switch, for, while, and do-while loops
  4. Functions: main(), return values, function prototypes, and parameter passing
  5. Arrays: one-dimensional and multi-dimensional arrays
  6. Pointers: understanding pointers, pointer arithmetic, and memory allocation
  7. File I/O: reading from and writing to files
  8. Standard library functions: printf(), scanf(), malloc(), free(), etc.

Core Concept

A variable in C is a named storage location that can hold data values. Each variable has a specific data type, such as int, char, or float, which determines the size and range of values it can store. In C, variables are declared using the data_type variable_name; syntax.

Declaring Variables

To declare a variable in C, you must specify its name (identifier) and data type:

int myInteger; // Declare an integer variable named 'myInteger'
char myCharacter; // Declare a character variable named 'myCharacter'
float myFloat; // Declare a floating-point variable named 'myFloat'

After declaring a variable, you can assign it a value using the assignment operator (=):

myInteger = 42; // Assign the integer variable 'myInteger' the value 42
myCharacter = 'A'; // Assign the character variable 'myCharacter' the ASCII value for 'A' (65)
myFloat = 3.14; // Assign the floating-point variable 'myFloat' the value 3.14

Variable Scope and Lifetime

In C, variables have a specific scope and lifetime:

  1. Local variables are declared within functions and exist only during the execution of that function. Once the function returns, local variables cease to exist.
  2. Global variables are declared outside any function and have global scope. They can be accessed from any function in the program. Global variables persist for the lifetime of the program.
  3. Static variables have a lifetime similar to global variables but only exist within their declaring function. Unlike local variables, static variables retain their values between function calls.

Variable Initialization

You can initialize a variable when you declare it by assigning an initial value:

int myInteger = 42; // Declare and initialize the integer variable 'myInteger' with the value 42
char myCharacter = 'A'; // Declare and initialize the character variable 'myCharacter' with the ASCII value for 'A' (65)
float myFloat = 3.14; // Declare and initialize the floating-point variable 'myFloat' with the value 3.14

If you do not initialize a variable, it will be automatically initialized to zero:

int uninitializedInteger; // The integer variable 'uninitializedInteger' is automatically initialized to 0
char uninitializedCharacter; // The character variable 'uninitializedCharacter' is automatically initialized to '\0' (NULL)
float uninitializedFloat; // The floating-point variable 'uninitializedFloat' is automatically initialized to 0.0

Worked Example

Let's create a simple C program that demonstrates the use of variables:

#include <stdio.h>

int main() {
int myInteger = 42; // Declare and initialize an integer variable named 'myInteger' with the value 42
char myCharacter = 'A'; // Declare and initialize a character variable named 'myCharacter' with the ASCII value for 'A' (65)
float myFloat = 3.14; // Declare and initialize a floating-point variable named 'myFloat' with the value 3.14

printf("myInteger: %d\n", myInteger); // Print the value of 'myInteger'
printf("myCharacter: %c\n", myCharacter); // Print the value of 'myCharacter'
printf("myFloat: %.2f\n", myFloat); // Print the value of 'myFloat' with two decimal places

return 0;
}

When you run this program, it will output:

myInteger: 42
myCharacter: A
myFloat: 3.14

Common Mistakes

  1. Variable naming: Avoid using reserved keywords and special characters as variable names. Use meaningful, descriptive names instead.
  2. Data type mismatch: Ensure you use the correct data type for your variables to avoid unexpected behavior or runtime errors.
  3. Uninitialized variables: Always initialize your variables to avoid undefined behavior caused by uninitialized variables containing garbage values.
  4. Variable scope: Be mindful of variable scope when accessing and modifying variables within functions.
  5. Memory leaks: Avoid memory leaks by properly deallocating memory allocated with malloc() or dynamically-sized arrays.

Practice Questions

  1. Declare and initialize three integer variables named age, height, and weight. Assign them the values 25, 70, and 68 respectively.
  2. Write a C program that reads two integers from the user and calculates their sum. Store the result in a variable called result.
  3. Write a C program that declares an array of five integers named numbers and initializes it with the values 1, 2, 3, 4, and 5. Print each element of the array on a separate line.

FAQ

  1. What happens if I don't initialize a variable in C? If you do not initialize a variable in C, it will be automatically initialized to zero for numeric types and '\0' (NULL) for character types.
  2. Can I declare multiple variables of the same type on the same line in C? Yes, you can declare multiple variables of the same type on the same line in C by separating them with commas: int a, b, c;
  3. What is the difference between local and global variables in C? Local variables are declared within functions and exist only during the execution of that function. Global variables are declared outside any function and have global scope, allowing them to be accessed from any function in the program.
  4. Can I change the data type of a variable once it has been declared in C? No, you cannot change the data type of a variable once it has been declared in C. If you need to store a different data type in a variable, you must declare a new variable with the appropriate data type.
  5. What is the purpose of the const keyword in C? The const keyword in C can be used to indicate that a variable's value should not be modified during program execution. Constants are immutable and have a fixed value throughout the program's lifetime.