Lesson 30 +10 XP

Data Types

Data Types

Before a program can store anything, it has to know what kind of thing it is storing. That's the job of a data type.

What is a type?

A type tells the compiler two things: how much memory the value uses, and how that memory should be interpreted.

The basic data types

C gives us a small set of built-in types:

int      // whole numbers
float    // decimal numbers
double   // decimal numbers, more precise
char     // a single character

Declaring variables of each type

int myNum = 5;              // integer
float myFloat = 5.99;       // decimal (float)
double myDouble = 19.99;    // decimal (double)
char myLetter = 'D';        // single character

Basic format specifiers

  • %d prints an int.
  • %f prints a float.
  • %lf prints a double.
  • %c prints a char.

Why "basic"?

These are the fundamental building blocks. C also has modifiers (short, long, unsigned, signed) that reshape them; we'll meet those next.

TL;DR

  • A type says how much memory a value uses and how to read it.
  • Basics: int, float, double, char.
  • Specifiers: %d, %f, %lf, %c.
  • Modifiers like short/long/unsigned tweak the basics.