There are lots of different types of numbers: integers (whole numbers without any decimal places), rational numbers, irrational numbers, complex numbers, etc. No matter how large, a computer’s amount of storage has limits. You can see that therefore it would be hard to store with 100% accuracy, a number with infinitely many decimal places such as \(\pi\text{.}\) Moreover, different types of numbers are dealt with in different ways internally and that’s why we discuss them separately. We start with whole numbers, also called integers, or int for short. Integers can be positive, negative, or zero and have no digits after the decimal point.
Up to this point, we have only printed words to the screen. In the following Codecast we’ll learn how to print numbers to the screen using the printf() statement and the format specifier for integers.
If you cannot see this codecast, please click here.
Check Your UnderstandingCheck Your Understanding
1.
What will be printed to the screen by the following program:
#include <stdio.h>
int main(void) {
printf("We need %d apples and %d cups sugar.\n", 10, 2);
return(0);
}
We need 10 apples and 2 cups of sugar.
Great job!
We need 10 apples and 10 cups of sugar.
Not quite - try again!
We need 2 apples and 2 cups of sugar.
Not quite - try again!
We need 2 apples and 10 cups of sugar.
Not quite - try again!
We need %d apples and %d cups of sugar.
Not quite - try again!
Hint.
There is a one-to-one match between format specifiers and parameters.