C Variables

📦 C Variables – Store and Use Data

In C programming, variables are used to store data that can be used and modified throughout the program. Think of variables as containers where you can stash your numbers, characters, or any data you need!

🔹 Declaring a Variable

To declare a variable, you need to specify its data type followed by its name. Optionally, you can assign a value right away.

data_type variable_name = value;

Example:

  • int age = 25;
  • float height = 5.9;
  • char grade = 'A';

📝 Example: Declare and Print Variables

This example shows how to declare variables and print their values.

#include <stdio.h>

int main() {
    int age = 25;
    float height = 5.9;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);
    printf("Grade: %c\n", grade);

    return 0;
}
  

Try It Now

✅ Rules for Naming Variables

  • Must start with a letter or underscore (_)
  • Can contain letters, digits, and underscores
  • No spaces or special characters
  • Cannot use C reserved keywords like int, return, etc.

🔁 Variable Declaration vs Initialization

  • Declaration: Tells the compiler about the variable.
    int score;
  • Initialization: Assigns an initial value.
    score = 90;
  • Can be combined: int score = 90;

🎯 Pro Tip

You can declare multiple variables of the same type in one line:
int x = 5, y = 10, z = 15;

🧪 Practice Challenge

Create a program that declares three variables: one int, one float, and one char, and prints them all out using printf().