You are on page 1of 55

C Input/Output Functions

Dr. Ajay Pratap

Assistant Professor

Department of Computer Science and Engineering

Indian Institute of Technology (BHU) Varanasi, India

ajay.cse@iitbhu.ac.in

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 1 / 32


Content of this Lecture

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 2 / 32


Content of this Lecture

Types of I/O
Console I/O Functions

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 2 / 32


Content of this Lecture

Types of I/O
Console I/O Functions
Formatted Console I/O Functions
sprintf( ) and sscanf( ) Functions

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 2 / 32


Content of this Lecture

Types of I/O
Console I/O Functions
Formatted Console I/O Functions
sprintf( ) and sscanf( ) Functions
Unformatted Console I/O Functions

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 2 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes
In input operations, the bytes flow from a device (e.g., a keyboard, a
disk drive, a network connection) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a
display screen, a printer, a disk drive, a network connection, and so on).

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes
In input operations, the bytes flow from a device (e.g., a keyboard, a
disk drive, a network connection) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a
display screen, a printer, a disk drive, a network connection, and so on).
Normally, the standard input stream is connected to the keyboard and
the standard output stream is connected to the screen.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes
In input operations, the bytes flow from a device (e.g., a keyboard, a
disk drive, a network connection) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a
display screen, a printer, a disk drive, a network connection, and so on).
Normally, the standard input stream is connected to the keyboard and
the standard output stream is connected to the screen.
Operating systems often allow these streams to be redirected to other
devices.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes
In input operations, the bytes flow from a device (e.g., a keyboard, a
disk drive, a network connection) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a
display screen, a printer, a disk drive, a network connection, and so on).
Normally, the standard input stream is connected to the keyboard and
the standard output stream is connected to the screen.
Operating systems often allow these streams to be redirected to other
devices.
A third stream, the standard error stream, is connected to the screen.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes
In input operations, the bytes flow from a device (e.g., a keyboard, a
disk drive, a network connection) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a
display screen, a printer, a disk drive, a network connection, and so on).
Normally, the standard input stream is connected to the keyboard and
the standard output stream is connected to the screen.
Operating systems often allow these streams to be redirected to other
devices.
A third stream, the standard error stream, is connected to the screen.
Error messages are output to the standard error stream.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


I/O in C

All input and output is performed with streams, which are sequences
of bytes
In input operations, the bytes flow from a device (e.g., a keyboard, a
disk drive, a network connection) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a
display screen, a printer, a disk drive, a network connection, and so on).
Normally, the standard input stream is connected to the keyboard and
the standard output stream is connected to the screen.
Operating systems often allow these streams to be redirected to other
devices.
A third stream, the standard error stream, is connected to the screen.
Error messages are output to the standard error stream.
Console I/O functions
File I/O functions

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 3 / 32


Console I/O functions

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 4 / 32


Console I/O functions
The screen and keyboard together are called a console.
Formatted and unformatted console I/O functions.
Formatted functions allow the input read from the keyboard or the
output displayed on the VDU (Video Display Unit) to be formatted as
per our requirements.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 4 / 32


printf( )

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 5 / 32


printf( )

printf ( ”format string”, list of variables );

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 5 / 32


printf( )

printf ( ”format string”, list of variables );


Characters that are simply printed as they are.
Conversion specifications that begin with a % sign
Escape sequences that begin with a \sign

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 5 / 32


printf( )

printf ( ”format string”, list of variables );


Characters that are simply printed as they are.
Conversion specifications that begin with a % sign
Escape sequences that begin with a \sign

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 6 / 32


Format Specifications
%d and %f used in the printf( ) are called format specifiers.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 7 / 32


Output of Integer Numbers

The format specification for printing an integer number of % wd


where, w specifies the minimum field width for the output.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 8 / 32


Output of Integer Numbers

The format specification for printing an integer number of % wd


where, w specifies the minimum field width for the output.
If the number is greater than the specified width, it will be printed in
full, overriding the minimum specification.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 8 / 32


Format Specifications

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 9 / 32


Format Specifications

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 10 / 32


Floating point

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 11 / 32


Floating point

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 12 / 32


Strings with printf( )

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 13 / 32


Escape Sequences

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 14 / 32


Escape Sequences

\b moves the cursor one position to the left of its current position.
\r takes the cursor to the beginning of the line in which it is currently
placed.
\a alerts the user by sounding the speaker inside the computer.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 15 / 32


An Example!

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 16 / 32


Printf(”%*d”, a, b)

”*” in the above means or is used to specify the length of space


required to print the integer.
printf takes two arguments one for space and other for integer.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 17 / 32


How does %*.*d work in printf()?

The argument to the * before the . is the field width and the argument to
the * after the . is the precision.
1
https:
//stackoverflow.com/questions/17566886/how-does-d-work-in-printf
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 18 / 32
scanf()

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 19 / 32


sprintf( ) Functions
Instead of sending the output to the screen as printf( ) does, sprintf(
) function writes the output to an array of characters.
In the following example: sprintf() stores the values of i, ch and a in
the character array str.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 20 / 32


sscanf( ) Functions
sscanf( ) allows us to read characters from a string and to convert
and store them in C variables according to specified formats.
The sscanf( ) function comes in handy for in-memory conversion of
characters to values.
Convenient to read in strings from a file and then extract values from
a string by using sscanf( )

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 21 / 32


sscanf( ) Functions
sscanf( ) allows us to read characters from a string and to convert
and store them in C variables according to specified formats.
The sscanf( ) function comes in handy for in-memory conversion of
characters to values.
Convenient to read in strings from a file and then extract values from
a string by using sscanf( )
A format: sscanf (String Variable Name, Format Specifier, variables);

Output: Name : Fresh2refresh


Age : 5
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 21 / 32
Unformatted Console I/O Functions

Nonstandard functions present in conio.h header file which is mostly


used by MS-DOS compilers like Turbo C.

1
POSIX: Portable Operating System Interface is a family of standards specified by
the IEEE Computer Society for maintaining compatibility between operating systems.
2
ANSI C, ISO C and Standard C are successive standards for the C programming
language published by the American National Standards Institute (ANSI) and the
International Organization for Standardization (ISO). Historically, the names referred
specifically to the original and best-supported version of the standard (known as C89 or
C90).
3
IDE:An integrated development environment is a software application that provides
comprehensive facilities to computer programmers for software development. An IDE
normally consists of at least a source code editor, build automation tools and a debugger.
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 22 / 32
Unformatted Console I/O Functions

Nonstandard functions present in conio.h header file which is mostly


used by MS-DOS compilers like Turbo C.
It is not part of the C standard library or ISO C, nor is it defined by
POSIX.

1
POSIX: Portable Operating System Interface is a family of standards specified by
the IEEE Computer Society for maintaining compatibility between operating systems.
2
ANSI C, ISO C and Standard C are successive standards for the C programming
language published by the American National Standards Institute (ANSI) and the
International Organization for Standardization (ISO). Historically, the names referred
specifically to the original and best-supported version of the standard (known as C89 or
C90).
3
IDE:An integrated development environment is a software application that provides
comprehensive facilities to computer programmers for software development. An IDE
normally consists of at least a source code editor, build automation tools and a debugger.
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 22 / 32
Unformatted Console I/O Functions

Nonstandard functions present in conio.h header file which is mostly


used by MS-DOS compilers like Turbo C.
It is not part of the C standard library or ISO C, nor is it defined by
POSIX.
These functions won’t run on Online compilers. Execute on MS-DOS
compilers like Turbo IDE.

1
POSIX: Portable Operating System Interface is a family of standards specified by
the IEEE Computer Society for maintaining compatibility between operating systems.
2
ANSI C, ISO C and Standard C are successive standards for the C programming
language published by the American National Standards Institute (ANSI) and the
International Organization for Standardization (ISO). Historically, the names referred
specifically to the original and best-supported version of the standard (known as C89 or
C90).
3
IDE:An integrated development environment is a software application that provides
comprehensive facilities to computer programmers for software development. An IDE
normally consists of at least a source code editor, build automation tools and a debugger.
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 22 / 32
Unformatted Console I/O Functions

getch( ) and getche( ) are two unformatted console I/O functions.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 23 / 32


Unformatted Console I/O Functions

getch( ) and getche( ) are two unformatted console I/O functions.


For some situations the scanf( ) function has one glaring weakness...
you need to hit the Enter key before the function can digest what you
have typed.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 23 / 32


Unformatted Console I/O Functions

getch( ) and getche( ) are two unformatted console I/O functions.


For some situations the scanf( ) function has one glaring weakness...
you need to hit the Enter key before the function can digest what you
have typed.
We often want a function that will read a single character the instant
it is typed without waiting for the Enter key to be hit.
getch( ) and getche( ) are two functions which serve this purpose.
These functions return the character that has been most recently
typed.
The ‘e’ in getche( ) function means it echoes (displays) the character
that you typed to the screen.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 23 / 32


Unformatted Console I/O Functions

getch( ) and getche( ) are two unformatted console I/O functions.


For some situations the scanf( ) function has one glaring weakness...
you need to hit the Enter key before the function can digest what you
have typed.
We often want a function that will read a single character the instant
it is typed without waiting for the Enter key to be hit.
getch( ) and getche( ) are two functions which serve this purpose.
These functions return the character that has been most recently
typed.
The ‘e’ in getche( ) function means it echoes (displays) the character
that you typed to the screen.
getchar( ) works similarly and echo’s the character that you typed on
the screen, but unfortunately requires Enter key to be typed following
the character that you typed.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 23 / 32


Unformatted Console I/O Functions
The difference between getchar( ) and fgetchar( ) is that the former is
a macro whereas the latter is a function.
fgetchar() is a file handling function in C programming language
which is used to get/read a single character from keyboard input.

1
Macros are pre-processed which means that all the macros would be processed
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 24 / 32
Unformatted Console I/O Functions

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 25 / 32


Unformatted Console I/O Functions
Working of putch( ) putchar( ) and fputchar( ) is exactly the same.
The limitation of putch( ), putchar( ) and fputchar( ) is that they can
output only one character at a time.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 26 / 32


Unformatted Console I/O Functions
Working of putch( ) putchar( ) and fputchar( ) is exactly the same.
The limitation of putch( ), putchar( ) and fputchar( ) is that they can
output only one character at a time.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 27 / 32


gets( ) and puts( )

gets( ) receives a string from the keyboard.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 28 / 32


gets( ) and puts( )

gets( ) receives a string from the keyboard.


Why is it needed?

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 28 / 32


gets( ) and puts( )

gets( ) receives a string from the keyboard.


Why is it needed?
Because scanf( ) function has some limitations while receiving string
of characters.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 28 / 32


gets( ) and puts( )
The moment blank was typed after “Jonty” scanf( ) assumed that the
name being entered has ended.
The solution to this problem is to use gets( ) function.
gets( ) gets a newline (\n) terminated string of characters from the
keyboard.

1
An array is collection of items stored at contiguous memory locations. The idea is
to store multiple items of same type together.
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 29 / 32
gets( ) and puts( )
The moment blank was typed after “Jonty” scanf( ) assumed that the
name being entered has ended.
The solution to this problem is to use gets( ) function.
gets( ) gets a newline (\n) terminated string of characters from the
keyboard.
The puts( ) function works exactly opposite to gets( ) function. It
outputs a string to the screen.

1
An array is collection of items stored at contiguous memory locations. The idea is
to store multiple items of same type together.
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 29 / 32
gets( ) and puts( )
The moment blank was typed after “Jonty” scanf( ) assumed that the
name being entered has ended.
The solution to this problem is to use gets( ) function.
gets( ) gets a newline (\n) terminated string of characters from the
keyboard.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 30 / 32


gets( ) and puts( )
The moment blank was typed after “Jonty” scanf( ) assumed that the
name being entered has ended.
The solution to this problem is to use gets( ) function.
gets( ) gets a newline (\n) terminated string of characters from the
keyboard.
The puts( ) function works exactly opposite to gets( ) function. It
outputs a string to the screen.

Output: Enter name


Jonty Rhodes
Happy footballing!
Jonty Rhodes
Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 30 / 32
Suggested Readings/Reference Materials

Chapters 11 (Console Input/Output) of:


Yashavant P. Kanetkar ”Let US C”, 5th Edition.

Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 31 / 32


Ajay Pratap (IIT (BHU), Varanasi) C input/output functions ajay.cse@iitbhu.ac.in 32 / 32

You might also like