C Variables

Understanding how to store and use data in C programming

📦 What are Variables?

Variables are containers that store data values. In C, you must declare a variable before using it, specifying its type and name.


// This is a simple C variable example
int age = 25;
printf("Age: %d", age);
                                    

Output:

Age: 25

Key Variable Concepts

🏷️

Declaration

Tell the compiler about variable type

int number;
🎯

Initialization

Assign a value to the variable

int number = 10;
📝

Assignment

Change the value after declaration

number = 20;
🔤

Naming Rules

Follow C naming conventions

int my_age;
int studentCount;

🔹 Variable Declaration and Initialization

Declare and initialize variables using various approaches suited to different programming scenarios. Initialize at declaration with int x = 5;, declare multiple variables like int x, y, z;, or assign later after declaration. Initializing at declaration prevents uninitialized variable bugs and improves code readability. Modern practice recommends initializing variables when declaring them. Proper initialization ensures predictable program behavior, prevents undefined behavior, and makes code easier for others to understand.

#include <stdio.h>

int main() {
    // Declaration only
    int age;
    
    // Declaration with initialization
    int score = 95;
    
    // Multiple declarations
    int x, y, z;
    
    // Multiple initializations
    int a = 1, b = 2, c = 3;
    
    // Assignment after declaration
    age = 25;
    
    printf("Age: %d\n", age);
    printf("Score: %d\n", score);
    printf("Sum: %d\n", a + b + c);
    
    return 0;
}

Output:

Age: 25
Score: 95
Sum: 6

🔹 Variable Naming Rules

Follow strict naming rules when creating variable names to ensure valid, readable, and maintainable code. Variable names must start with a letter or underscore, containing only letters, digits, and underscores. Names are case-sensitive; myVar differs from myvar. Avoid reserved keywords like int, for, or return. Use descriptive names clarifying variable purpose. Following naming conventions improves code readability, prevents naming conflicts, and makes collaboration with other programmers smoother.

✅ Valid Names:

  • Start with letter (a-z, A-Z) or underscore (_)
  • Can contain letters, digits, and underscores
  • Case-sensitive (age and Age are different)

❌ Invalid Names:

  • Cannot start with a digit
  • Cannot contain spaces or special characters
  • Cannot use C keywords (int, if, while, etc.)
// Valid variable names
int age;
int student_count;
int _temp;
int myAge2;

// Invalid variable names
// int 2age;        // starts with digit
// int my age;      // contains space
// int int;         // C keyword

🔹 Common Variable Types

The most frequently used variable types in C cover most programming scenarios and applications. int handles general integer operations, float or double for decimal numbers, char for characters and text, and unsigned int for non-negative integers. Pointers int* manage memory and dynamic data structures. Understanding when to use each type enables choosing appropriate types for specific problems, optimizing memory usage, and writing efficient, correct C programs.

#include <stdio.h>

int main() {
    // Integer variables
    int whole_number = 42;
    
    // Floating-point variables
    float decimal_number = 3.14f;
    double precise_decimal = 3.14159265359;
    
    // Character variables
    char letter = 'A';
    char grade = 'B';
    
    // Display all variables
    printf("Integer: %d\n", whole_number);
    printf("Float: %.2f\n", decimal_number);
    printf("Double: %.10f\n", precise_decimal);
    printf("Character: %c\n", letter);
    printf("Grade: %c\n", grade);
    
    return 0;
}

Output:

Integer: 42
Float: 3.14
Double: 3.1415926536
Character: A
Grade: B

🧠 Test Your Knowledge

Which of the following is a valid variable name in C?