0% found this document useful (0 votes)
98 views16 pages

11-Formatted Output Functions PDF

This document discusses formatted output functions in C programming. It explains that the printf() function is used to display data with formatting by using format specifiers in a control string. Some key points covered are: - printf() moves data from memory to standard output while scanf() moves data from standard input to memory. - The general form of printf() includes a control string with format specifiers and any number of arguments. - Common format specifiers are %d for integers, %f for floating point numbers, %c for characters, and %s for strings. - Examples are given to demonstrate how different data types can be formatted and output using printf().

Uploaded by

Vishwanathan S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
98 views16 pages

11-Formatted Output Functions PDF

This document discusses formatted output functions in C programming. It explains that the printf() function is used to display data with formatting by using format specifiers in a control string. Some key points covered are: - printf() moves data from memory to standard output while scanf() moves data from standard input to memory. - The general form of printf() includes a control string with format specifiers and any number of arguments. - Common format specifiers are %d for integers, %f for floating point numbers, %c for characters, and %s for strings. - Examples are given to demonstrate how different data types can be formatted and output using printf().

Uploaded by

Vishwanathan S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

Programming Logic

Formatted
Output
Functions
B.Bhuvaneswaran, AP (SG) / CSE
9791519152
bhuvaneswaran@rajalakshmi.edu.in
printf() Function
 The calculated results are output with the help of output function
called printf().
 This function is used to display any combination of digits,
characters and strings.
 It is similar to the scanf() function, but the purpose is to display
the data.
 The printf() function moves data from the computer’s memory to
the standard output device whereas the scanf() function enters
data from the standard input device and stores it in the
computer’s main memory.

Formatted Output Functions Rajalakshmi Engineering College 2


General Form
 printf("control strings", arg1, arg2, . . . argn) ;
 Where the control strings consists of three types of items:
• Characters that will be printed on the screen as they appear.
• Format specifications that define the output format for display of each
item.
• Escape sequence characters such as \n and \t.
 where the arguments arg1, arg2, . . . argn are the variables whose
values are formatted and printed according to the specifications of
the control string.
 The arguments should match in number, order and type with the
format specification.

Formatted Output Functions Rajalakshmi Engineering College 3


Rules
 All the format specifiers (control strings) are written within double
quotes.
 Each format specifier (control string) must be preceded by the %
sign.
 There must be a format specifier corresponding to every variable.
 The order of the format specifier is same as the order of the
corresponding variables.

Formatted Output Functions Rajalakshmi Engineering College 4


Output of Integer Numbers
 The format specification for printing an integer number is:
%d
or
%wd
 where w specifies the minimum field width for the output.
 However, if a number is greater than the specified field width, it
will be printed in full, overriding the minimum specification.
 d specifies that the value to be printed is an integer.

Formatted Output Functions Rajalakshmi Engineering College 5


Example and Explanation
 Consider the following statement :
printf("%d %d %d", a, b, c);
 Suppose the input data items are: a = 10, b = 20 and c = 30, the
system displays the following output:
10 20 30

Formatted Output Functions Rajalakshmi Engineering College 6


Input of Real Numbers
 The format specification for printing a real number in decimal
notation is:
%f or %w.pf or %w.pf
 The format specification for printing a real number in exponential
notation is:
%e or %w.pe or %.pe
 where the integer w indicates the minimum number of positions
that are to be used for the display of the value and the integer p
indicates the number of digits to be displayed after the decimal
point (precision), the default precision is 6 decimal places.

Formatted Output Functions Rajalakshmi Engineering College 7


Example and Explanation
 Consider the following statement:
printf("%f %.2f %10.2f", a, b, c);
 Suppose the input data items are: a = 12.3456, b = 98.7654 and c =
13.5791, the system displays the following output :
12.3456 98.76 1.36e+01

Formatted Output Functions Rajalakshmi Engineering College 8


Output of Single Character
 A single character can be displayed in a desired position using the
format:
%c
or
%wc
 The character will be displayed right justified in the field of w
columns.
 We can make the display left justified by placing a minus sign
before the integer w, the default value for w is 1.

Formatted Output Functions Rajalakshmi Engineering College 9


Example and Explanation
 Consider the following statement :
printf("%c", gender);
 Suppose the input data item is: gender = 'M', the system displays
the following output:
M

Formatted Output Functions Rajalakshmi Engineering College 10


Output of Strings
 The format specification for outputting strings is similar to that of
real numbers. It is of the form:
%s
or
%w.ps
 where w specifies the field width for display and p instructs that
only the first p characters of the string are to be displayed.

Formatted Output Functions Rajalakshmi Engineering College 11


Example and Explanation
 Consider the following statement:
printf("%s", name);
 Suppose the input data item is: name = "Bhuvan", the system
displays the following output:
Bhuvan

Formatted Output Functions Rajalakshmi Engineering College 12


Output of Mixed Mode Data Types
 It is possible to use one printf() statement to output the values
containing mixed mode data such as integer, float and char.
 In such cases, care should be exercised to ensure that the format
specifications should match the variables in number, order and
type.

Formatted Output Functions Rajalakshmi Engineering College 13


Example and Explanation
 Consider the following statement:
printf("%d %s %c %.2f", id, name, gender, salary);
 Suppose the input data items are: id = 101, name = "Bhuvan",
gender = 'M' and salary = 6000, the system displays the following
output :
101 Bhuvan M 6000.00

Formatted Output Functions Rajalakshmi Engineering College 14


Different printf() Format Codes
Format Specifier Type of Argument Output
%c Character Prints a single character
%d or %i Integer Prints a signed decimal integer
%e Floating point Prints a floating point value in exponent form
%E Floating point Same as e, with E for exponent
%f Floating point Prints a floating point value without exponent
Prints a floating value either e or f form depending on the value or
%g Floating point
precision
%G Floating point Same as G, with E for exponent if e format is used
%hd or %hi Short integer Prints decimal short integer
%hu Short integer Prints decimal unsigned short integer
%ld or %li Long integer Prints decimal long integer
%le or %lf or %lg Double Prints signed double
%Le or %Lf or %Lg Long double Prints signed long double
%lo Long integer Prints an octal long integer
%lu Long integer Prints decimal unsigned long integer
%lx Long integer Prints hexadecimal long integer
%o Octal integer Prints an octal integer, without leading zero
%s Sequence of characters Prints a string until a null (\0) character is reached
%u Integer Prints an unsigned decimal integer
%x Hexadecimal integer Prints a hexadecimal integer (with a, b, c, d, e, f) without leading 0x

%X Hexadecimal integer Prints a hexadecimal integer (with A, B, C, D, E, F) without leading 0X

Formatted Output Functions Rajalakshmi Engineering College 15


Thank You

You might also like