🎓 A1Learner
🔍
0 of 50 lessons completed

C Data Types

The variables lesson used int for whole numbers. A data type says what a variable can hold, and C has four basic types: int, float, double, and char.

Four types in one program

Four variables hold four kinds of value. Each print shows one value, with the float and double printed to six decimal places.

#include <stdio.h>

int main() {
  int count = 42;
  float price = 9.99f;
  double pi = 3.14159;
  char grade = 'A';
  printf("%d\n", count);
  printf("%f\n", price);
  printf("%f\n", pi);
  printf("%c\n", grade);
  return 0;
}

A char holds one letter

first holds H and second holds i. The two letters print side by side as Hi.

#include <stdio.h>

int main() {
  char first = 'H';
  char second = 'i';
  printf("%c%c\n", first, second);
  return 0;
}

Decimal values need decimal types

whole holds 5, and the two decimal variables hold 2.5. All three prints show the values, with the decimals printed to six places.

#include <stdio.h>

int main() {
  int whole = 5;
  float small = 2.5f;
  double big = 2.5;
  printf("%d\n", whole);
  printf("%f\n", small);
  printf("%f\n", big);
  return 0;
}

Example

#include <stdio.h>

int main() {
  int year = 2026;
  float temperature = 21.5f;
  double distance = 1234.5678;
  char unit = 'C';
  printf("Year: %d\n", year);
  printf("Temperature: %f\n", temperature);
  printf("Distance: %f\n", distance);
  printf("Unit: %c\n", unit);
  return 0;
}

Key points

  • A type says what a variable can hold.
  • int holds whole numbers, like 42 and -7.
  • float holds a number with a decimal point, like 9.99.
  • double also holds a number with a decimal point, and it holds more digits than float.
  • char holds one single character, like A or 7.
  • On most computers today, int takes 4 bytes and char takes 1 byte.
  • float takes 4 bytes, and double takes 8 bytes on most computers today.
  • An int holds whole numbers from -2,147,483,648 to 2,147,483,647.
  • A float holds about 6 to 7 decimal digits, and a double holds about 15 to 16.
  • Printing a float with %f shows six digits after the decimal point.

Four values print, each with its label. The output shows the year, the temperature, the distance, and the unit, each in its own type.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which type holds a number with a decimal point and keeps the most digits?