C printf()

Last update on January 25 2017 11:38:35 (UTC/GMT +8 hours)

Formatted Output and the printf function

One of the common task in every program is the printing of output. We use the output to request input from a user and later display the status/result, computations etc. In C programming there are several functions for printing formatted output. Here we discuss the printf() function, which writes output to the computer monitor. To use the printf() function we must include the stdio library in the source code. To do this just place the following code at the beginning of your program.

#include <stdio.h>

To print a simple message in computer screen you might call printf() function as follows :

Output:

If we want to print the second output in a new line we must need a different way, which has discussed in the following section.

Escape sequences in C

An escape sequence is a series of characters that represents a special character. It begins with a backslash character (\), which indicates that the character(s) that follow the backslash character should be treated in a special way. C uses escape sequences within a format string to print certain special characters. For example \n moves the output position to the beginning of the next line. The following is a list of escape sequences.

Escape sequence

Action

\n

prints a new line

\b

backs up one character

\t

moves the output position to the next tab stop

\\

prints a backslash

\"

prints a double quote

\'

prints a single quote

You will get an idea of using the above escape sequences from the following example.

#include<stdio.h>
main()
{
printf("Create a new line\n");
printf("Print a double quotes (\") within a string\n");
printf("Print a single quotes (\') within a string\n");
printf("Print a Backslash\\ within a string\n");
printf("Using Backspace\b within a string\n");
printf("Using\tTab within a string\n");
}

Output :

Create a new line
Print a double quotes (") within a string
Print a single quotes (') within a string
Print a Backslash\ within a string
Using Backspace within a string
Using Tab within a string

Using printf() to print values

In the above section, we have discussed how to print a new line, single quote, double quote etc. In this section, we will discuss how to print the value of a variable. There are various format available to print different type values. Let start with these codes :

int x;
x = 150;
printf("Number of students in Class V is %d",x);

In the printf() function the format parameter '%d' is replaced with the value of the parameter x. Therefore the output looks like : Number of students in Class V is 150. In addition to %d, there are quite a number of format specifiers, each having a different meaning. Here are the basic ones (several others exist) :

Parameter

Meaning

%d

Print an integer number printed in decimal (preceded by a minus sign if the number is negative).

%f

Print a floating point number ( in the form dddd.dddddd).

%E

Print a floating point number ( in scientific notation: d.dddEddd).

%g

Print a floating point number (either as f or E, depending on value and precision).

%x

Print an integer number in hexadecimal with lower case letters.

%X

Print an integer number printed in hexadecimal with upper case letters.

%c

Print a character.

%s

Print a string.

The following code prints two numbers one is positive another is negative.

Output:

Using format specifier options in printf

You can specify the width and precision of numbers and strings as they are inserted. See the syntax :

%[flags][width][.precision]Parameter

For example, %4c means the entire field (three blanks and one non-blank character) occupies 4 columns, %7.2f means to print a float or double in a field at least 7 spaces wide, with two places to the right of the decimal.