WeCodeAcademy

C Variables and Types

A variable stores a value and must be given a type: int (whole numbers), float and double (decimals), char (one character). In printf, format specifiers say how to show each value: %d for int, %f for float or double, %c for char, %s for text.

ExampleC
#include <stdio.h>

int main(void) {
  int age = 36;
  double height = 1.68;
  char grade = 'A';
  const int YEAR = 2026;   // const: cannot change

  printf("Age: %d\n", age);
  printf("Height: %.2f\n", height);
  printf("Grade: %c\n", grade);
  printf("%d is %d years from %d\n", age, YEAR - 2000, YEAR);

  printf("int uses %zu bytes, double uses %zu bytes\n", sizeof(int), sizeof(double));
  return 0;
}
Output
Age: 36
Height: 1.68
Grade: A
36 is 26 years from 2026
int uses 4 bytes, double uses 8 bytes
Quick check

Which format specifier prints an int?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.