KevsRobots Learning Platform

Getting Started with C on the Raspberry Pi Pico

30% Percent Complete

Variables and Data Types

Learn how to store and manipulate data in C using variables and different data types.

By Kevin McAleer,    3 Minutes

Page last updated June 15, 2025


Cover


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.


What Is a Variable?

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:

  1. Choose a type (what kind of data you want to store)
  2. Choose a name for the variable (like age or temperature)
  3. Optionally set an initial value

Basic Data Types in C

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

Declaring Variables

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!

Comments in C

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 */

Arithmetic in C

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

C is a Typed Language

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.


Summary

  • Variables let you store and use values in your programs
  • C requires you to define data types (int, float, char, etc.)
  • You can do math and print values using printf

Next up: Conditionals and Loops — where we’ll teach your program to make decisions and repeat things!


< Previous Next >

You can use the arrows  ← → on your keyboard to navigate between lessons.