0% found this document useful (0 votes)
18 views

Chapter 4

This document discusses input and output operations in C programming. It covers both unformatted and formatted I/O. Unformatted I/O deals with character input/output using functions like getchar(), putchar(). Formatted I/O allows reading and writing of various data types using format specifiers, with scanf() and printf() being the main functions. Examples are given of programs that use unformatted and formatted I/O to get user input and display output on the console. As an exercise, students are asked to write programs demonstrating the use of different I/O statements.

Uploaded by

Pawan Poudel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Chapter 4

This document discusses input and output operations in C programming. It covers both unformatted and formatted I/O. Unformatted I/O deals with character input/output using functions like getchar(), putchar(). Formatted I/O allows reading and writing of various data types using format specifiers, with scanf() and printf() being the main functions. Examples are given of programs that use unformatted and formatted I/O to get user input and display output on the console. As an exercise, students are asked to write programs demonstrating the use of different I/O statements.

Uploaded by

Pawan Poudel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter:4

Input and Output


Er. Saroj Shakya
IOE, Thapathali Campus
Contents
4. Input and Output (2 hours)
4.1 Formatted I/O
4.2 Character I/O
4.3 Programs Using I/O statement

IOE, Thapathali Campus 6/6/2021 2


Input and Output in Program
▪ Suppose a scenario where you have to take input numbers from the keyboards and
sum them in your program before displaying it on the screen.
▪ Here, Input and Output operations must exist.
▪ Input and Output (I/O) are not part of C Language, instead, these operations are
supplied as functions in the standard library.
▪ More than a dozen header files which describe the property of for the I/O operations.
▪ <stdio.h>, <conio.h>, <string.h>, <ctype.h> etc are few frequently used library files.
(For more details follow any C book)
▪ Two type of I/O
▪ Unformatted I/O
▪ Formatted I/O

IOE, Thapathali Campus 6/6/2021 3


Unformatted I/O (Character I/O)
▪ Unformatted console input functions are used to read a single input from the user
at console
▪ Unformatted console output allows us to display the value in the output to the
user at the console.
▪ Unformatted input and output functions are only work with character data type.
▪ Unformatted input and output functions do not require any format specifiers
(covered in formatted I/O) because they only work with character data type.
▪ Some functions for unformatted I/O are:
▪ getchar(): reads character type data form the input. The getchar() function reads one
character at a time till the user presses the enter key. The getchar() reads the input from
the user and display back to the user.

IOE, Thapathali Campus 6/6/2021 4


Unformatted I/O (Character I/O)
▪ Some functions for unformatted I/O are:
▪ a = getchar(): reads character type data form the input. The getchar() function reads one
character at a time till the user presses the enter key. The getchar() reads the input from the
user and display back to the user.
▪ a = getch(): reads the alphanumeric character input from the user. But, that the entered
character will not be displayed.
▪ a = getche(): reads the alphanumeric character from the user input. Here, character you
entered will be echoed to the user until he/she presses any key.
▪ putchar(‘Q’): prints only one character at a time.
▪ putch(‘Q’): prints any alphanumeric character.
▪ gets(b): read a full string even blank spaces presents in a string.
▪ puts(“Hello World!!”): prints the character array or string on the console. We cannot print
other than characters using puts() function.

IOE, Thapathali Campus 6/6/2021 5


Formatted I/O
Formatted Input:
▪ Formatted input refers to an input of data that has been arranged in a particular
format.
▪ scanf() function is used for formatted input. General form is
scanf(“control string”, arg1, arg2, arg3);
▪ The control string specifies the field format in which data is to be entered and the
arguments arg1, arg2.. argn specify the address of locations where the data is stored.
▪ control string contains field specifications consisting of the conversion character %, a
data type character and an optional number specifying the field width.

IOE, Thapathali Campus 6/6/2021 6


Formatted I/O
Formatted Output:
▪ printf() function provide features that can be used to control the aligned and
spacing of printouts on screen. The general form of print is as follows:
printf(“control string”, arg1, arg2, arg3);
▪ The control string specifies the field format in which data is to be entered and the
arguments arg1, arg2.. argn specify the address of locations where the data is stored.
▪ control string contains field specifications consisting of the conversion character %, a
data type character and an optional number specifying the field width.

IOE, Thapathali Campus 6/6/2021 7


Formatted I/O
Code Meaning
%c Read a single character
%d Read a decimal number integer
%e, %E Read a floating point value
%f Read a floating point value
%g, %G Read a floating point value
%h Read a short integer
%i Read a decimal, hexadecimal or octal integer (auto detects the base)
%o Read an octal integer
%x, %X Read a hexadecimal number without leading 0x
%u Read an unsigned decimal number
%s Read a string

IOE, Thapathali Campus 6/6/2021 8


Formatted Output Escape Sequences
Character Meaning
▪ Some special backslash
\a Bell Alert
characters constants that are
used in output function are \b backspace

called escape sequeces. \t Horizontal tab


\v Vertical tab
▪ Escape sequences are \n New line
always begins with a
\” Quotation mark
backward slash and is
followed by one or more \’ Aoistrophe
special characters. \? Question mark
\\ Back slash (confusion with comment? Comment: //)
▪ Some of escape sequences
\0 Null
are shown in table.
\r Carriage return

IOE, Thapathali Campus 6/6/2021 9


Programs Using Unformatted Output statement

IOE, Thapathali Campus 6/6/2021 10


Programs Using Unformatted Input statement

IOE, Thapathali Campus 6/6/2021 11


Programs Using Formatted Output statement

IOE, Thapathali Campus 6/6/2021 12


Programs Using Formatted Input statement

IOE, Thapathali Campus 6/6/2021 13


Programs Using Formatted Input statement

IOE, Thapathali Campus 6/6/2021 14


Programs Using Un/Formatted Input statement
Class work, time: 30 mins (30 points)
▪ Write a c program using formatted Output to display the today’s date and time in
DD/MM/YYYY HH:MM:SS AM/PM format in your console screen.
▪ Write a c program using formatted I/O to display the student’s info like string
roll(“THA077BEI122”), integer age(18), float weight(55.253), character gender
(‘M’/’F’/’O’), string name (single word). These info should be entered from
keyboard in console.
▪ Write a c program using unformatted and formatted I/O for 2nd problem. (name is
full name in this case).

IOE, Thapathali Campus 6/6/2021 15

You might also like