Formatted and Unformatted Functions
Formatted and Unformatted Functions
• Input/Output functions
Outline
• Formatted Input/Output functions
– printf() function
– scanf() function
• Conversion Specifiers
Introduction
• Presentation of output is very important.
• Formatted functions scanf and printf :
– these functions input data from standard input
stream and
– output data to standard output stream.
• Include the header #include<stdio.h>
Streams
• Streams are sequence of bytes.
• In input operations, the bytes flow from a
device(e.g. keyboard, disk drive) to main
memory.
• In output operations, bytes flow from main
memory to device(e.g. display screen, printer).
Standard I/O Functions
• There are many library functions available for
standard I/O.
• These functions are divided into two
categories:
–Unformatted functions
–Formatted functions
Formatted Functions
• With Formatted functions, input and output is
formatted as per our requirement
– For example, if different values are to be displayed, how
much field width i.e., how many columns on screen, is
to be used, and how much space between two values is
to be given. If a value to be displayed is of real type,
then how many decimal places to output
• Formatted functions are:
– printf()
– scanf()
Unformatted functions
• The unformatted functions work only with character data
type.
• They do not require format conversion symbol for formatting
of data types because they work only with character data type
• Unformatted functions are:
– getchar() and putchar()
– getch() and putch()
– gets() and puts()
Formatted output with printf function
• The printf() function: (Formatted output)
printf() is an output function that takes text and values from within
the program and sends it out onto the screen.
In general terms, the printf function is written as:
Syntax
printf(“format-control-string”, arg1,arg2,……….,argN);
890
890
890
-890
32000
200000000
890
3246435674
Printing Floating-Point number
• Decimal point numbers like 0.01, 98.07 or -23.78
Conversion Description Example
Specifier
e or E Display a floating-point value in printf(“%e”,-1234567.89);
exponential notation.
f or F Display floating-point values in fixed- printf(“%f”,1234567.89);
point notation
g or G Display a floating-point value in either printf(“%g”, 1234567.89);
the floating-point from f or the
exponential form e based on the
magnitude of the value
L Used before any floating-point printf(“%lf”,1234567.89L);
conversion specifier to indicate that a
long double is displayed.
#include <stdio.h>
int main( void )
{
printf( "%e\n", 1234567.89 );
printf( "%e\n", -1234567.89 );//minus prints
printf( "%E\n", 1234567.89 );
printf( "%f\n", 1234567.89 );
printf( "%g\n", 1234567.89 );
printf( "%G\n", 1234567.89 );
}
1.234568e+006
-1.234568e+006
1.234568E+006
1234567.890000
1.234568e+006
1.234568E+006
Printing Strings and Characters
Character = ‘A’ and String= “This is string”
Conversion Description Example
Specifier
c Display a single character argument. printf(“%c”, ‘A’);
s Displays a string and requires a pointer printf(“%s”, “This is
to a character argument. String”);
printf( "%c\n",
character );
printf( "%s\n",
"This is a string" );
printf( "%s\n", string );
A
This is string
This is string
Other Conversion Specifier
Pointer holds the address of another variable.
Conversion Description Example
Specifier
p Display a pointer value Int *ptr=&score;
printf(“%p”, ptr);
printf(“%p”, &score);
% Displays the percent character. printf(“a%%”);
#include <stdio.h>
int main( void )
{
int *ptr; // define pointer to int
int x = 12345; // initialize int x
123
1234
12345
-123
-1234
-12345
How?
• Dividing 7 by 3
Answer :
2.33333333333……
But the required output is
2.3333
Printing with Precision
• Specifies precision with which data is printed.
• Precision with integer conversion specifier indicates
the minimum number of digits to be printed.
• Precision with floating-point conversion specifier
indicates the number of digits to appear after the
decimal point.
• Precision with string specifier indicates the
maximum number of characters to be written from
the string.
#include <stdio.h>
int main( void )
{
int i = 873; // initialize int i
double f = 123.94536; // initialize double f
char s[] = "Happy Birthday"; // initialize char array s