C Data Types
Data Types
As explained in the Variables chapter, a variable in C must be a specified
data type,
and you must use a format specifier inside the printf()
function to display it:
Example
// Create variables
int myNum = 5; // Integer (whole number)
float
myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; //
Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
Try it Yourself »
Basic Data Types
The data type specifies the size and type of information the variable will store.
In this tutorial, we will focus on the most basic ones:
Data Type | Size | Description |
---|---|---|
int |
2 or 4 bytes | Stores whole numbers, without decimals |
float |
4 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits |
double |
8 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits |
char |
1 byte | Stores a single character/letter/number, or ASCII values |
Basic Format Specifiers
There are different format specifiers for each data type. Here are some of them:
Format Specifier | Data Type | Try it |
---|---|---|
%d or %i |
int |
Try it » |
%f |
float |
Try it » |
%lf |
double |
Try it » |
%c |
char |
Try it » |
%s |
Used for strings, which you will learn more about in a later chapter | Try it » |