printf ( ) function:
Data
is displayed from the computer onto a standard output device using the library
function printf ( ). Its main purpose is to display data from the computer’s
memory to the standard output device in a specified format. It also falls under
the category of formatted I/O functions.
printf ( ) can be used to display
any combination of output such as numerical value’s single characters and
strings. It is almost similar to that of scanf ( ) except that its purpose is
to display data rather than to enter data into computer.
General Syntax is:
printf (formatstring, arg1, arg2,…….., argn);
The
formatstring is a string that contains the required formatting information. It
consists of one or more character groups where each character group is used for
each data item to be entered.
The
various conversion characters that can be used are:
d – Displays
an integer in decimal form.
e – Displays a floating point number in exponential form.
f - Displays
a floating point number in fixed decimal format i.e without an exponenet.
The
arguments arg1, arg2,……., argn are the arguments that represent the individual
data items. They can be single variable, array names or expression. Unlike
scanf ( ), the arguments in
printf
( ) are not preceded by ampersands (&). This is because they don’t
represent memory address.
To Know the use of printf (
) let us consider a program.
#include<stdio.h>
#include<conio.h>
void
main()
{
int age;
printf(“\n Enter your age :”);
scanf(“%d”,&age);
printf(“\n I live in America “);
printf(“\n I am %d years old”,age);
getch();
}
Output:
Enter your age : 17
I live in America
I am 17 years old
The printf ( ) function
examines the format string from left to right.
Post a Comment