# C – printf() Features and Format

Course Outline  | Previous LessonNext Lesson

### Introduction

The printf function displays text to the screen. A newline character breaks up texts from one to multiple lines, and function printf can also display data from variables. In addition, printf can be customized by displaying text in certain ways as designed by software developers. Printf does not only display text to the screen. Like the main function, the printf function returns a value that signifies how many bytes of data used to display to the screen. Printf is an integer function like the main function. More of that will be discussed later in this lesson.

Newline is what people see when they press “Enter” on their keyboards. Newline is represented as \n. That symbol is considered to be one character, and that character can be placed in the string of printf. Please see the code below that uses the newline character.

#include <stdio.h>

int main() {
printf("This is cool!\nAm I right?!");
return 0;
}

That newline character above will not be displayed to the screen. Instead, a line-break replaces the newline character. The following output is:

This is cool!

Am I right?!

That newline character is important for organizing string for display. It line-breaks string from one to multiple lines. In this case, the string is broken into two lines. It is a good practice to include the newline character at the end of every string for every printf function. Every example codes below will follow that practice.

### Displaying Integers with Variables

There is a way to display an integer variable to the screen. The code below demonstrates that.

#include <stdio.h>

int main() {
int example = 2;

printf("The number is %d\n", example);
return 0;
}

When the computer execute the code above, it will replace %d with an integer 2 in the printf statement. As a result, the program will output The number is 2. In general, the %d is a placeholder for an integer variable, and the letter d stands for decimal. The %d is like a variable inside of the string. This is the correct printf syntax for displaying an integer value from a variable. The %i can also be used since it is the same thing as %d. The code above can also be written like the code below.

#include <stdio.h>

int main() {
printf("The number is %d\n", 2);
return 0;
}

#### Argument

An argument means that there is a value that is being sent to a function. In the example code above, the printf function has two arguments inside of its parentheses, and the comma separates them. The string literal is the first argument, and the second argument is the integer 2. Those two arguments are being sent to the printf function, and that function manipulates the string literal by replacing %d with an integer 2. It is important to know what an argument is.

#### Multiple Variables with printf

The following code below will output two values from two variables. The printf function below has three arguments.

#include <stdio.h>

int main() {
int example = 2;
int example2 = 20;

printf("Two values: %d, %d\n", example, example2);
return 0;
}

The computer will use the first variable to replace the first %d with its value, and the second variable will replace the second %d with its value as well. In addition, more variables can be added to printf. Just be sure that there is one %d per each integer variable. The output is Two values: 2, 20.

### Formatting Integer Values

The %d can be modified to change the way the computer displays the integer from the string in the printf function. Please refer to the code below to see an example of that.

#include <stdio.h>

int main() {
int example = 3;

printf("Number is%2d\n", example);
return 0;
}

That 2 from %2d represents the number of spacing that could be added to the left of the variable’s value. The 2 from %2d does not guarantee two spaces to the left. The %2d depends on the difference between 2 from the placeholder itself and the number of digits from the variable’s value. If there are more than two digits from the variable’s value, there would not be any spacing, which would render 2 useless.

In this example, however, we only have one digit for the variable’s value. To find the number of spacing that will be added, calculate $2-1$ where 2 and 1 represents the number from the placeholder (%2d) and the number of digits from the variable’s value respectively. As a result of the calculation, this will give 1. This means that only a spacing of one will be applied. Thus, the output of this snippet of code is Number is 3.

#### Add Spacing to the Right

Instead of adding spacing to the left, spacing can be added to the right. The %2d would become %-2d. Please refer to the code below.

#include <stdio.h>

int main() {
int example = 1;

printf("%-2dis the number\n", example);
return 0;
}

The output above is 1 is the number.

#### Replace Spacing with Leading Zeros

#include <stdio.h>

int main() {
int example = 1;

printf("The number is%02d\n", example);
return 0;
}

The output for this snippet of code is The number is01. Note that zeros cannot be added to the right of any number. That would change the number completely.

#### Generalization

Innstead of just having %02d, there can be %-1000d, %092000d, and so on. To generalize this, there can be %fmd where f and m are any integers. The letter f is a variable that represents a flag that signifies where spacing may apply or when to use zeros instead. The letter m is a variable that represents the minimum field width of any integer. For an example, the 2 from %-2d is m.

#### Short Int

The %d need to be modified even further if certain sizes for int are need. The code below will demonstrate how to display short int.

#include <stdio.h>

int main() {
short int example = 1;

printf("Short int: %hd\n", example);
return 0;
}

The h from %hd denotes that the number comes from a short integer variable. The h has to be to the left of d.

#### Long Int

Long int can also be displayed with the printf function. The code below demonstrates how to output that kind of data type.

#include <stdio.h>

int main() {
long int example = 1;

printf("Long int: %ld\n", example);
return 0;
}

The letter l represents long. The letter l needs to be to the left of d to output long integer data type.

#### Long long int

The code below demonstrates how to output long long data types.

#include <stdio.h>

int main() {
long long int example = 1;

printf("Long long int: %lld\n", example);
return 0;
}

To output long long data type, two letters, l, need to be used.

#### Unsigned int

For displaying unsigned integer values, d is replaced with u. Thus, %d becomes %u. It is simple as that.

#### Further Generalization

To generalize the placeholder even further, %fmd or %fmu becomes %fmLd or %fmLu respectively where L is used for short int, long int, or long long int.

### Displaying Real Numbers with Variables

Variables of data types float and double can also be displayed with the printf function. They both use the placeholder %f. When a float data type is being used, the printf function makes a copy of that data type and “upgrades” that copy to double. The original variable itself is still float. Not double.

#### Formatting Float or Double

Real numbers can also be formatted the same as integers. However, a real number can have digits after a decimal point like 1.006 for an example. Sometimes, there has to be a limitation of digits after the decimal point to represent things like money. Imposing limitations like that can be done in C. Please refer to the code below.

#include <stdio.h>

int main() {
float example = 1.006;

printf("You have $%.2f\n", example); return 0; } In this example, the program displays the amount of money a user have. Money is usually represented with two digits after the decimal point. The code above is imposing a limitation in which the number can have only two digits after the decimal point. A period has to be included to denote that limitations may be imposed to digits after the decimal point. The number 2 represents the maximum number of digits after the decimal point. Printf also rounds the number as well. The output is You have$1.01.

#### Numbers with Scientific Notation

Numbers with scientific notation like $1.7\times10^{20}$ can be used in the printf function as well. In programming, however, numbers like that has to be typed in this way: $1.7e20$. The code below goes over a way to display numbers with scientific notation.

#include <stdio.h>

int main() {
double variable = 1.7e20;

printf("Number: %f\n", variable);
return 0;
}

The output of the code above is Number: 170000000000000000000.000000.

#### Adding Commas for Big Numbers

Software developers can add commas to the string literal for big numbers, but the task is too complicated for this lesson. They would need to use a special variable that stores strings. That special variable can be manipulated by adding commas within the string. The printf function can use that variable with placeholder %s. That process will be covered in a future lesson.

#### Display Numbers in Scientific Notation

What if software developers want to display the numbers in scientific notation without converting them to plain, real numbers? That can be done, but placeholder %f cannot be used in that case. Placeholder %e or %E has to be used instead. Please refer to the code below.

#include <stdio.h>

int main() {
double example = 1.7e20;

printf("Number: %e or %E\n", example);
return 0;
}

The output would be Number: 1.700000e+020 or 1.700000E+020. Placeholder %e represents the lowercase e, and placeholder %E represents the uppercase E. That is the only difference between %e and %E. In addition, the output can be Number: 1.7e+020 or 1.7E+020 when placeholders are modified with %.2e and %.2E respectively.

There is no trivial way to limit the number of digits after e or E. Software developers would have to use a special variable that stores strings. That variable’s value can be manipulated to limit the number of digits after e or E. Remember that that variable can be used for printf with the %s placeholder. However, a future lesson will cover that process since it is too complicated at this time.

#### Numbers with Various Size

In some cases, software developers do not want to use scientific notation for small numbers, but they do want to use it for big numbers. When users can input numbers to a program, developers cannot predict how big the numbers would be. Developers want to find a way where printf can automatically determine when to use scientific notation and regular notation. That can be done with %g or %G. Please refer to the code below.

#include <stdio.h>

int main() {
double example = 1.7;

printf("%g or %G\n", example);
return 0;
}

The output of the code above is 1.7 or 1.7 since 1.7 is a small number. The code below will use big numbers.

#include <stdio.h>

int main() {
double example = 1700000;

printf("%g or %G\n", example);
return 0;
}

The code above outputs 1.7e+006 or 1.7E+006 since it is a big number. This is how useful %g and %G are. Those placeholders automatically determine whether to use the scientific notation or not. As a result, the placeholders choose one of the notations to obtain the smallest number of digits that represents the same overall number. This means that 1.7 is smaller than 1.7e0 (length wise), and 1.7e6 is smaller than 1700000 (length wise as well).

#### Small Numbers with Scientific Notations

Same rules above apply to small numbers. Numbers like $1.7\times10^{-20}$ can be written as 1.7e-20.

### Displaying Char with Variables

Function printf can also output a character from variables. The code below demonstrates that.

#include <stdio.h>

int main() {
char example = 'A';

printf("Letter: %c\n", example);
return 0;
}

The output is Letter: A. The code below is the same as the one above.

#include <stdio.h>

int main() {
char example = 65;

printf("Letter: %c\n", example);
return 0;
}

The output is still Letter: A. The number 65 is the ASCII code for the letter A.

If placeholder %c is used, data type int can still be used to convert a number to a letter. Please refer to the code below for an example.

#include <stdio.h>

int main() {
int example = 65;  // or int example = 'A';

printf("%c or %d\n", example, example);
return 0;
}

The output is A or 65. Remember that char and int data types have different sizes. That is the only difference. That is how int data type can be displayed as a character instead.

### Final Format Generalization

The generalized placeholder for printf is %fm.pLX where p represents the maximum amount of digits to the right of the decimal point and where X represents any data type (specifier) that would be used like d (for int).

### Table of Basic Specifiers

The table below summarizes all, basic specifiers that displays values from variables with all data types.

### Return Value of printf

Printf is an integer function that returns the number of bytes the text has. Note that a letter or a character takes up exactly one byte of data. A space counts as a character as well.

#include <stdio.h>

int main() {
int example = printf("This is fun.\n");
return 0;
}

The string in the printf function above has 13 bytes. As a result, the printf function returns an integer 13, and that value is being stored into the variable above. Remember that \n is only one character. Thus, that newline character is only one byte. In addition, the computer does not count the double quotes. Furthermore, the code will look like the one below right after the printf statement is done executing.

#include <stdio.h>

int main() {
int example = 13;
return 0;
}

#### Return Value of Printf with Variables

The printf function also works when variables are used to display their values.

#include <stdio.h>

int main() {
int example1 = 1;
int example2 = printf("Value: %d\n", example1);
return 0;
}

The printf function above returns an integer 9 because the computer only counts the number of characters in between the double quotes. In addition, the %d is not being counted because it is being replaced with an actual integer. Instead, the integer is being counted. Since the integer only has one digit, the overall number of bytes is 9. However, if example1 has an integer 19, the overall number of bytes from the string literal of printf is 10. That return value is being stored into example2.

At this point, beginners may point out that integer variables usually take up 4 bytes of memory. Not one nor two. That is true, but the printf function is makes a copy of the variable’s value, and that copy converts to a string. When the integer variable has a value of 10, each digit converts to a character literal. Thus, the number of characters from 10 is 2.

### Conclusion

The printf function gives programmers a way to display text to the screen while including the variables’ values via placeholders. Furthermore, it can also allow programmers to customize the way variable’s value looks. Finally, printf is an integer function that returns the number of bytes in the string.

Course Outline  | Previous LessonNext Lesson