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:
// 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 »
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 6-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 |
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 (text), which you will learn more about in a later chapter | Try it » |
You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point:
float myFloatNum = 3.5;
double myDoubleNum = 19.99;
printf("%f\n", myFloatNum); // Outputs 3.500000
printf("%lf", myDoubleNum); // Outputs 19.990000
Try it Yourself »
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:
float myFloatNum = 3.5;
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
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!