C Tutorial



VARIABLES IN C


Variables in C

A variable in C is a name given to a memory location that stores data. It acts like a container for data that your program can manipulate. Each variable must be declared with a specific data type.

๐Ÿ“˜ Remember: Variables must be declared before they are used.

๐Ÿ“Œ Variable Declaration Syntax

data_type variable_name;
  

Or declare and initialize in one line:

data_type variable_name = value;
  

๐Ÿงช Example

#include <stdio.h>

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

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

    return 0;
}
  

๐Ÿงฑ Variable Types

  • int - for integers (e.g., 5, -3)
  • float - for decimal numbers (e.g., 3.14)
  • char - for single characters (e.g., 'A')
  • double - for large/precise decimal numbers

โœ… Rules for Naming Variables

  • Must start with a letter (Aโ€“Z or aโ€“z) or underscore (_)
  • Can contain letters, digits (0โ€“9), and underscores
  • No special symbols like @, $, etc.
  • Cannot use reserved C keywords (e.g., int, return)
๐Ÿ’ก Tip: Use meaningful names. For example, use marks instead of m to make code more readable.

โŒ Common Mistakes

  • ๐Ÿšซ Using undeclared variables
  • ๐Ÿšซ Misspelling variable names
  • ๐Ÿšซ Using wrong format specifiers in printf (e.g., using %d for a float)

๐Ÿ“ Try It Yourself

  1. Declare 3 variables: name (char), marks (float), roll_no (int)
  2. Assign values and print them using printf

๐Ÿง  Variable Tip: You can declare multiple variables of the same type in one line:
int a = 5, b = 10, c = 15;


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review