HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Data Types

Data Types

int, float/double, char, and ASCII; format specifiers and type casting.

Simple enough for a 5-year-old to understand

Character and ASCII

In C, char is actually a small number (ASCII code). %c prints as a character, %d as a number.

Example code
1
#include <stdio.h>
2
int main() {
3
char letter = 'A';
Character 'A'.
VariableCharacter 'A'.
4
printf("%c\n", letter);
A
Output / InputA
5
printf("%d\n", letter);
65 (ASCII code!)
Output / Input65 (ASCII code!)
6
return 0;
7
}
VariableOutput / Input
💡 Hint: 'A' = 65, 'a' = 97. char and int can convert to each other.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the ASCII numerical value of the character 'A' using %d.

🎯 Expected output: 65

Loading editor…

About this topic

Types in C determine how many bytes of memory are occupied and how those bytes are interpreted. int holds an integer, float and double hold decimal numbers, and char holds a single character. There was no separate type for boolean values for a long time: in C, zero is false, and anything other than zero is true.

The size of types is not fixed in the standard; it varies by platform. int is four bytes on most systems, but this is not guaranteed. When an exact size is needed, the names in the stdint.h header are used; these carry the number of bytes in their names.

Here is C's most error-prone behavior: it performs silent conversions between types. When you assign a double value to an int variable, the decimal part is discarded without a warning. The compiler does not warn you because it assumes you are doing this intentionally.

What you will learn

  • Basic types and the value ranges they hold
  • Why integer division loses decimals
  • That printf format specifiers must match the type
  • What happens in case of overflow
  • Measuring byte size with sizeof

When to use?

Do not use float and double if you are doing money calculations. Decimal numbers cannot be represented exactly in binary; rounding errors accumulate in cent calculations. Keeping the amount as an integer in cents is the correct solution.

Type selection is important in embedded systems because memory is constrained: using int for an eight-bit value wastes four times the space.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    int result = 7 / 2;
    printf("%d\n", result);
    return 0;
}

Division of two integers is an integer: the result is 3, not 3.5, and the decimal part is discarded without rounding. This does not give an error message; it silently produces a wrong result. For a decimal result, at least one of the operands must be decimal.

Correct

#include <stdio.h>

int main() {
    double result = 7.0 / 2;
    printf("%f\n", result);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    double price = 19.5;
    printf("%d\n", price);
    return 0;
}

Format specifier does not match the type: percent f is used for a double value, and percent d for an integer. When they do not match, printf interprets the memory incorrectly and prints a meaningless number or the program crashes. This is undefined behavior; the compiler may give a warning but does not prevent it.

Correct

#include <stdio.h>

int main() {
    double price = 19.5;
    printf("%f\n", price);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    char letter = "A";
    printf("%c\n", letter);
    return 0;
}

Double quotes mean text, single quotes mean a character. Writing with double quotes produces a string address and cannot be assigned to a char variable; the compiler gives a type mismatch error. Single quotes are always used for a single character.

Correct

#include <stdio.h>

int main() {
    char letter = 'A';
    printf("%c\n", letter);
    return 0;
}

Frequently asked questions

What happens if the int limit is exceeded?

Overflow in a signed integer is undefined behavior: the compiler can do whatever it wants, and the value usually turns negative. In unsigned types, however, the behavior is defined and the value wraps around. Use long long for large numbers, and you can access the limits from the limits.h header.

What is the difference between float and double?

double takes up twice as much space and holds about twice as many significant digits. Decimal constants in C are double by default, so unless there is a special reason, using double is both more accurate and more natural.

Is there a boolean type in C?

Since the 1999 standard, you can use bool, true, and false by including the stdbool.h header. In the background, these are integers. You can write conditions even without this header because C considers zero false and any other value true.

Summary

The type determines the number of bytes allocated in memory and how the bytes are interpreted.

Integer division silently discards the decimal part.

The printf format specifier must match the type of the value.

Single quotes mean a character, double quotes mean a string.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code