C STRUCTURAL PROGRAMMING
(I/O STATEMENTS)
1 Abdul Wahab
Lecturer in IT,
IECS, UST Bannu
[email protected]
INPUT STATEMENT
The statements used to provide data to the
program during its execution are called input
statements
The following statements are used in C/C++ to
input data into the program:
scanf() function
gets function
getch(() function
getche() function
2
OUTPUT STATEMENT
The statements that are used to get data from
the program and send it to an output device are
called output statements
The following functions are used in C/C++ to send
data to an output device:
printf() function
puts() function
3
THE PRINTF() FUNCTION
The printf() function is used to print values and
text on the output device in a specified format.
It is also called the formatted output function
The general syntax of this function is:
printf (control_string [, list of arguments]);
4
THE PRINTF() FUNCTION CONTD..
The Control String consists of text, the format
specifiers and the escape sequence. It is written
inside double quotes:
The text specifies the message that is to be
printed along with the values
A format specifier specifies the format according
to which a value is to be printed
5
THE PRINTF() FUNCTION CONTD..
The list of arguments consists of a list of:
Variables
Constants
Arithmetic expressions
They are separated by commas
The values are printed according to the
corresponding format specifier specified in the
control string
6
FORMAT SPECIFIER
A format specifier specifies the data type, field
width and format of a value.
It is used to specify the format according to which
a value is to be printed
% sign is used to indicate the beginning of the
format specifier
% sign is followed by a conversion character
which is used to convert an output value to a
desired data type. 7
CONVERSION CHARACTER FOR INTEGER
Character Description
i The output is converted to signed integer value
d The output is converted to signed decimal integer value
o The output is converted to unsigned octal integer value
u The output is converted to unsigned decimal integer
value
x The output is converted to unsigned hexadecimal value
with lower : a, b, c, d, e, f
X The output is converted to unsigned hexadecimal value
with uppercase letters: A, B, C, D, E, F
8
CONVERSION CHARACTER FOR
CHARACTERS
Character Description
c It is used for a single character
s It is used for string
9
CONVERSION CHARACTER FOR FLOATING
POINT
Character Description
f The output is converted to signed real value
e The output is converted to signed real value using e-
notation
E The output is converted to signed real value E-notation
g It is used for large floating point value using e-notation or
f form, which ever is shorter
G It is used for large floating point value using E-notation
or F form, which ever is shorter
10
ESCAPE SEQUENCE
Special characters used to control printing on the
output device
It is a combination of a backslash „\‟ and a
control character. Where the backslash is called
the control character
For example: to transfer the printing control to
next line the escape sequence is written as \n
11
COMMONLY USED ESCAPE SEQUENCE CHARACTERS
Character Explanation
\a • Used for Alert
• Generate Beep sound in internal Speaker
\b • Stands for backspace
• Used to bring the printing control one space back
\t • Stands for Tab
• Used to transfer the printing control one Tab forward
\n • Used to move the control to the beginning of next line
\f • Stands for form feed
• Used for printing one blank paper on the printer
\r • Stands for carriage return
• It moves the cursor to the beginning of the current line
\\ Used to print a backslash
\” Used to print double quotation marks
12
\‟ Used to print single quotation mark
GETS() FUNCTION
This function is used to get input data into a
string variable from key board
Any type of characters including spaces and
special characters can be entered
Its syntax is:
gets( strVar );
Where strVar represents a string type variable
into which data is to be entered
13
GETCH() FUNCTION
It stands for “Get Character”
This function is used to get a single character
from keyboard during execution of program
The entered character is not displayed on the
screen
It is normally used to pause the execution of
program in console based compilers
14
GETCHE() FUNCTION
It stands for “Get Character and Echo”
It is similar to getch() function with a difference
that it displays the entered character during the
execution of program
15
SCANF() FUNCTION
This function is used to get values into variables
from keyboard during program execution
The value is entered into a variable in s specified
format. Its syntax is;
scanf(control_string, list of arguments);
Control string specifies the format specifiers as
discussed before. Unlike control string in printf()
strings cannot be given here 16
SCANF() FUNCTION CONTD…
List of variable is separated by commas, all
variables are proceeded by ampersand (&) sign
which denoted the memory address of the
variable
& sign is also called the address operator
For example:
scanf(“%d %f %d”, &a, &f, &b);
17
PUTS() FUNCTION
It is used to print a string on the computer
screen.
Its syntax is:
puts(str);
The str may be string constant or string type
variable:
Staring variable is written without using quotes
String constant is enclosed in double quotes
For example:
puts(“This a string Constant”); 18
HAVE A GOOD DAY
19