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);
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 | Example |
---|---|---|---|
int |
2 or 4 bytes | Stores whole numbers, without decimals | 1 |
float |
4 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits | 1.99 |
double |
8 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits | 1.99 |
char |
1 byte | Stores a single character/letter/number, or ASCII values | 'A' |
Basic Format Specifiers
There are different format specifiers for each data type. Here are some of them:
Format Specifier | Data Type |
---|---|
%d or %i |
int |
%f or %F |
float |
%lf |
double |
%c |
char |
%s |
Used for strings (text), which you will learn more about in a later chapter |
Note: It is important that you use the correct format specifier for the specified data type. If not, the program may produce errors or even crash.
The char Type
The char
data type is used to store a single character.
The character must be surrounded by single quotes, like 'A' or 'c', and we use the %c
format specifier to print it:
Example
char myGrade = 'A';
printf("%c", myGrade);
Alternatively, if you are familiar with ASCII, you can use ASCII values to display certain characters. Note that these values are not surrounded by quotes (''
), as they are numbers:
Example
char a = 65, b = 66, c = 67;
printf("%c", a);
printf("%c", b);
printf("%c", c);
Notes on Characters
If you try to store more than a single character, it will only print the last character:
Note: Don't use the char
type for storing multiple characters, as it may produce errors.
To store multiple characters (or whole words), use strings (which you will learn more about in a later chapter):
Example
char myText[] = "Hello";
printf("%s", myText);
For now, just know that we use strings for storing multiple characters/text, and the char type for single characters.
The Numeric Types
Use int
when you need to store a whole number without decimals, like 35 or 1000, and float
or double
when you need a floating point number (with decimals), like 9.99 or 3.14515.
int
int myNum = 1000;
printf("%d", myNum);
float
float myNum = 5.75;
printf("%f", myNum);
double
double myNum = 19.99;
printf("%lf", myNum);
float
vs. double
The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float
is six or seven decimal digits, while double
variables have a precision of about 15 digits. Therefore, it is often safer to use double
for most calculations - but note that it takes up twice as much memory as float
(8 bytes vs. 4 bytes).
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the power of 10:
Example
float f1 = 35e3;
double d1 = 12E4;
printf("%f\n", f1);
printf("%lf", d1);
Decimal Precision
Set Decimal Precision
You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point:
Example
float myFloatNum = 3.5;
double myDoubleNum = 19.99;
printf("%f\n", myFloatNum); // Outputs 3.500000
printf("%lf", myDoubleNum); // Outputs 19.990000
If you want to remove the extra zeros (set decimal precision), you can use a dot (.
) followed by a number that specifies how many digits that should be shown after the decimal point:
Example
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n", myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits
C The sizeof Operator
Get the Memory Size
We introduced in the data types chapter that the memory size of a variable varies depending on the type:
Data Type | Size |
---|---|
int |
2 or 4 bytes |
float |
4 bytes |
double |
8 bytes |
char |
1 byte |
The memory size refers to how much space a type occupies in the computer's memory.
To actually get the size (in bytes) of a data type or variable, use the sizeof
operator:
Example
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
Note that we use the %lu
format specifer to print the result, instead of %d
. It is because the compiler expects the sizeof operator to return a long unsigned int
(%lu
), instead of int
(%d
). On some computers it might work with %d
, but it is safer to use %lu
.
Why Should I Know the Size of Data Types?
Knowing the size of different data types is important because it says something about memory usage and performance.
For example, the size of a char
type is 1 byte. Which means if you have an array of 1000 char
values, it will occupy 1000 bytes (1 KB) of memory.
Using the right data type for the right purpose will save memory and improve the performance of your program.
You will learn more about the sizeof
operator later in this tutorial, and how to use it in different scenarios.
Real-Life Example
Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
Example
// Create variables of different data types
int items = 50;
float cost_per_item = 9.99;
float total_cost = items * cost_per_item;
char currency = '$';
// Print variables
printf("Number of items: %d\n", items);
printf("Cost per item: %.2f %c\n", cost_per_item, currency);
printf("Total cost = %.2f %c\n", total_cost, currency);