0% found this document useful (0 votes)
40 views3 pages

2-Input Output Statements

The document discusses standard input/output (I/O) functions in C for unformatted and formatted character and string input/output. It describes common functions like getchar(), puts(), scanf(), printf() and their usage. For unformatted I/O, it covers character functions getchar(), getche(), getch(), putchar() and string functions gets(), puts(). For formatted I/O, it explains the scanf() and printf() functions and provides examples of format specifiers used with each.

Uploaded by

ADITYA KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

2-Input Output Statements

The document discusses standard input/output (I/O) functions in C for unformatted and formatted character and string input/output. It describes common functions like getchar(), puts(), scanf(), printf() and their usage. For unformatted I/O, it covers character functions getchar(), getche(), getch(), putchar() and string functions gets(), puts(). For formatted I/O, it explains the scanf() and printf() functions and provides examples of format specifiers used with each.

Uploaded by

ADITYA KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

‘P.S.

T & Programming in C’ [I/O Statements]

Standard I/O Functions


There are many library functions available for console I/O. These functions are
divided into two categories : ● Unformatted and ● Formatted
The only difference between these functions is that, the formatted functions permit
the input from a standard output device to be formatted as per requirement.

Unformatted I/O functions


In the unformatted I/O category, we have functions for performing input/output of one
character at a time, known as character I/O functions and functions that perform input/output
of one string at a time, known as string I/O functions.
Character I/O Functions
getchar() function – This function returns a character that has been recently typed. The
typed character is echoed to the computer screen. After typing the appropriate character, the
user is required to type ENTER key. Syntax variable_name = getchar();
getche() function – Like getchar() function, this function also returns a character that has
been recently typed. The typed character is also echoed to the computer screen. But the user
is not required to press ENTER key after typing a character.
The advantage of this function over getchar() function is that as soon as the user types
a character, the character is immediately accepted. Syntax variable_name=getche();
getch() function–This function too returns a character that has been recently typed. But,
neither the user is required to type ENTER key nor the typed character echoed to the
computer screen. This function has advantages over the other functions in the applications
where the user wants to hide the input. Syntax variable_name=getch();
This input is usually for the password, used for system security.
putchar() function – This function outputs the character constant or character variable to the
standard output device. Syntax putchar(variable name)

String I/O Functions


For string input, function gets() is used,and for string output, the function puts() is
used.
gets() function – This function accepts a string from the standard input device. The length of
the string is limited by the declaration of the string variable. The input may consist of
multiple words. The input is completed by typing ENTER key.
puts() function – This function outputs a string constant or a string variable to the standard
output device.

Formatted I/O functions


In the formatted I/O category, scanf() function is for input and printf() function is for
output.

scanf() function – This function allows us to input data in a fixed format


scanf(―format string‖, list of addresses of variables);
Where format string contains the format specifiers that begins with character ‗%‘ and
are separated by space or comma.

The list of addresses of variables are used so that scanf() function can place the data
received from a standard input device. The address of a variable is obtained by using address
operator.

Rajeshwar Dayal <[email protected]> Page 1


‘P.S.T & Programming in C’ [I/O Statements]

Format Specifier Used for


%d Integer value
%f Real value
%e Floating point (exponential notation)
%c Single character
%s String
%ld Signed long integer
%lf Double precision floating point
%u Unsigned decimal integer
%x Unsigned hexadecimal integer
%o Unsigned octal integer

Limitation of scanf() function


scanf() has a limitation, that it only reads a word. As soon as it encounters a white
space, stops the reading. Therefore, it takes string of only one word, not the multi word
string.
So, to input a multiword string we have to use gets() function.

printf() function – The printf() function allows to output the data in a fixed format. Its
general form is printf(―format string‖, list of variables);
The printf() function examines the format string from left to right. It keeps on sending
the characters to the standard output device until it encounters either character ‗%‘ or
character ‗\‘(backslash). The moment it encounters %, it picks up the corresponding variable
and outputs its value in the specified format.

The format string of the printf() function contains format specifiers, the sequences
that begin with character ‗\‘ (back slash), known as escape sequences, and the text to be
output alongwith the output data.

All the format specifiers used with scanf() functions are valid for printf() function.

Common Escape Sequences


Escape Sequence Represents Effects
\n New Line Subsequent output starts from new line
\t Tab Moves over to the next 8-space wide field (Tab)
\b Backspace Moves the cursor one space left
\r Carriage Return Carriage Return
\f Form feed Advances to the top of the next page on printer
\a Beep Produces one audio tone of the speaker
\‘ Single quote Inserts character ‗ in the output
\‖ Double quotes Inserts character ― in the output.
\\ Back Slash Inserts character \ in the output.

Rajeshwar Dayal <[email protected]> Page 2


‘P.S.T & Programming in C’ [I/O Statements]

As we see in the output, that the printf() function outputs six (6) decimal digits for a single
precision real value, because in the format specifier, any additional information is not
specified. In general, to have a control over the output, additional information can be passed
in printf() function.

With this additional information, the format specifier for a real value takes the form
%w.df Where w is total width or space used for outputting a real value
including decimal point and the number of decimal digits; and d is the number of decimal
digits to be outputted.
For an integer value %wd
For a string value %ws

By default, the output value always right justified in the field. It is also possible to
output a value left unjustified in the field by writing hyphen ‗—‗ after character ‗%‘ as shown
below
%-5d
int a = 125;
printf(―%6d‖,a); 1 2 5

printf(―% - 6d‖,a); 1 2 5

Exercise
1. Write a program in c to add three values given by the user.
2. Write a program in C to calculate area of rectangle and perimeter.
3. Write a program in C to convert temperature given into Fahrenheit into Celsius.
4. Write a program to convert the time given in seconds to Hour,Minutes and Seconds.
5. Write a program to find the sum of digits of a 4-digit integer number.
6. Write a program to print 4-digits positive integer number in reverse order.

Rajeshwar Dayal <[email protected]> Page 3

You might also like