The printf() function in <stdio> library is one alternate way to print something in the console!
To simply print something, we call the function and cast our string inside.
printf("Hello World");Substitution
One of the most powerful things printf() can do is substitution.
If we have to display something in the console like:
Player 1: <<name>>
However, we want to replace <<name>> with any player name we like, perhaps something that is stored in a variable.
We can utilize substitution using this line of code.
char name[] = "Steve";
printf("Player 1: %s", name); // "Player 1: Steve"We can use other specifiers to substitute certain data types.
| Specifier | Data Type |
|---|---|
%d | Integer/Digit |
%f | Floating Point |
%s | String |
%c | Character |
Here’s some few examples:
int grade = 90;
printf("You got a grade of %d!\nKeep up the good work!", grade);float eNum = 2.718281828459045;
printf("The Euler\'s is %f", eNum);Floating-Point Precision
Sometimes, we don’t want to display the entire decimal part of an integer.
In this case, we can use the format %.nf to display how many decimal places do we want.
We simply replace n with how many decimal places to display.
float pound = 2.2046226;
printf("1 kilogram is %.2f pounds.", pound); // "1 kilogram is 2.20 pounds."If the floating point number doesn’t have enough decimal places that is required to be displayed in %.nf, then the remaining decimal places are replaced with zeros.
float num = 3.5 // has only 1 decimal place
printf("%.5f", num) // requires 5 decimal places (Output: 3.50000) Base Conversion
An integer can be converted to different number bases using these special specifiers.
For example, if we use %X, we convert the integer into the hexadecimal number system.
printf("%o", 15) // Outputs 17
printf("%X", 15) // Outputs FHere’s the list of what we can use for base conversion:
| Specifier | Number System |
|---|---|
%d or i | Signed Decimal Integer |
%u | Unsigned Decimal Integer |
%x | Hexadecimal (lowercase) |
%X | Hexadecimal (uppercase) |
%o | Octal |
String Specifiers
In a string, we can also have width and precision.
We can set the minimum width for a string by using the %ns width modifier.
If we substitute a string whose length is less than n, then, it adds spaces " " until it reaches the minimum length.
printf("%5s", "Hello") // "Hello"
printf("%10s", "Hello") // "Hello "In the second line, since "Hello" already has 5 characters, it then adds 5 more spaces to reach the minimum width 10.
Conversely, we can also set a maximum width of a string by using the %.ns precision modifier.
If we substitute a string whose length is greater than n, it then cuts the string until the substituted part only displays n characters long.
printf("%.5s", "Hello, world!") // "Hello" Multiple Substitution
We can also substitute multiple variables in a string!
Suppose we want to substitute the name and playerNumber into the string:
Player <<playerNumber>>: <<name>>
We can also do it like this:
int playerNumber = 1;
char name[] = "Steve";
printf("Player %d: %s", playerNumber, name); // Outputs "Player 1: Steve"The variables we want to substitute must also be in correct order.
For example, if we have the string "Grade %d: %s - %s", then we should also place the variable for %d first, then the first %s, then the rightmost %s, in that order
printf("Grade %d: %s - %s", 11, "TVL", "ICT"); // Outputs "Grade 11: TVL - ICT"