0
0
Cprogramming~5 mins

Basic data types

Choose your learning style9 modes available
Introduction

Basic data types help us tell the computer what kind of information we want to store, like numbers or letters.

When you want to store whole numbers like age or count.
When you need to store decimal numbers like price or temperature.
When you want to store a single character like a letter or symbol.
When you want to store true or false values.
When you want to store text using arrays of characters.
Syntax
C
int myNumber;
float myDecimal;
char myLetter;
bool myFlag;  // requires #include <stdbool.h>

int stores whole numbers.

float stores decimal numbers.

char stores a single character.

bool stores true or false values (needs #include <stdbool.h>).

Examples
Stores the whole number 25 in a variable named age.
C
int age = 25;
Stores the decimal number 19.99 in price.
C
float price = 19.99;
Stores the letter 'A' in grade. Note the single quotes for characters.
C
char grade = 'A';
Stores a true/false value in isOpen. You must include stdbool.h to use bool.
C
#include <stdbool.h>
bool isOpen = true;
Sample Program

This program shows how to use basic data types in C. It prints each value with a message.

C
#include <stdio.h>
#include <stdbool.h>

int main() {
    int age = 30;
    float temperature = 36.6f;
    char initial = 'J';
    bool isSunny = true;

    printf("Age: %d\n", age);
    printf("Temperature: %.1f\n", temperature);
    printf("Initial: %c\n", initial);
    printf("Is it sunny? %s\n", isSunny ? "Yes" : "No");

    return 0;
}
OutputSuccess
Important Notes

Remember to use single quotes for char values, like 'A'.

Use %d in printf for int, %f for float, and %c for char.

Boolean values print as 1 (true) or 0 (false) by default, so use a conditional to print words.

Summary

Basic data types store different kinds of simple information.

int for whole numbers, float for decimals, char for single characters, and bool for true/false.

Use the right type to help the computer understand your data.