Labs ICT
โญ Pro Login

Variables

1 min read | C Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

Variables are how you store data in C. Think of them as labeled boxes. You put a value in a box, give it a name, and later you can look inside that box or change what is in it.

Declaring a Variable

In C, you have to declare a variable before you can use it. Declaration tells the compiler: "Hey, I need a box named age that holds an integer."

The syntax is: type name;


int age;
float price;
char letter;
    

Naming Rules

C has strict rules for variable names:

  • Names can contain letters, digits, and underscores
  • Names must start with a letter or underscore โ€” never a digit
  • Names are case-sensitive: age and Age are different
  • No spaces or special characters like @, #, $
  • You cannot use C keywords like int, return, if as names

Assigning and Printing

You assign a value with =. You print with printf and format specifiers like %d for integers and %f for floats.


#include <stdio.h>

int main() {
  int age = 25;
  float price = 19.99;
  char grade = 'A';

  printf("Age: %d\n", age);
  printf("Price: %.2f\n", price);
  printf("Grade: %c\n", grade);
  return 0;
}
    

๐Ÿงช Quick Quiz

Which of these is a valid C variable name?