In C, as outlined in the Variables section, each variable must have a defined data type. Additionally, when displaying variables using the printf() function, it’s necessary to include a format specifier.
// 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); |
The data type determines the size and nature of the information stored in a variable.
This tutorial will primarily cover fundamental data types.
Data Type |
Size |
Description |
Example |
|
2 or 4 bytes |
Stores integers, excluding decimals. |
|
|
4 bytes |
Stores floating-point numbers, which include one or more decimals. Can accommodate approximately 6-7 decimal digits. |
|
|
8 bytes |
Stores floating-point numbers, which include one or more decimals. Can accommodate approximately 15 decimal digits. |
|
|
1 byte |
Stores a single character, letter, number, or ASCII value. |
|
Various data types have distinct format specifiers associated with them. Here are a few examples:
Format Specifier |
Data Type |
|
|
|
|
|
|
|
|
|
Reserved for strings (text), details of which will be covered in a subsequent chapter. |