KevsRobots Learning Platform
30% Percent Complete
By Kevin McAleer, 3 Minutes
Page last updated June 15, 2025

To make your programs useful, you need to store data, work with numbers, and control logic.
Thatβs where variables and data types come in.
In this lesson, weβll explore how to declare variables in C and which data types are available.
A variable is a named piece of memory that stores a value.
Think of it as a labeled box where you can keep something β like a number or a letter.
In C, you must:
age or temperature)| Type | Description | Example Value |
|---|---|---|
int |
Integer number | 42 |
float |
Decimal number (approx.) | 3.14 |
char |
Single character | 'A' |
bool |
True/false (with stdbool.h) |
true |
Hereβs how to declare and use variables:
#include <stdio.h>
#include <stdbool.h> // needed for bool
int main() {
int age = 25;
float temperature = 23.5;
char grade = 'B';
bool isOn = true;
printf("Age: %d\n", age);
printf("Temperature: %.1f\n", temperature);
printf("Grade: %c\n", grade);
printf("Is it on? %d\n", isOn);
return 0;
}
Try it: Change the values and run the program again!
Notice the // at the end of the line in the code above?
Thatβs a comment! Comments are ignored by the compiler and are used to explain your code. In C, comments start with // for single-line comments or /* ... */ for multi-line comments.
// This is a single-line comment
/* This is a
multi-line comment */
You can do math using these operators:
| Symbol | Meaning | Example |
|---|---|---|
+ |
Add | a + b |
- |
Subtract | a - b |
* |
Multiply | a * b |
/ |
Divide | a / b |
% |
Modulo (remainder) | a % b |
In C, the type of a variable cannot change once itβs declared.
int age = 30;
age = "thirty"; // β Error: incompatible type
Note: You must declare a variable before using it.
printfNext up: Conditionals and Loops β where weβll teach your program to make decisions and repeat things!
You can use the arrows β β on your keyboard to navigate between lessons.
Comments