Sei sulla pagina 1di 16

Input and Output

statements
DR. SHARIFAH ADZILA BINTI SYED ABU BAKAR
FACULTY OF MECHANICAL & MANUFACTURING ENGINEERING

I/O single character


getchar()
putchar(variable)

same letter will appear

input function scanf and the output function printf.

I/O of string
gets( string)
puts(string)

same input will be output

General input (scanf)


To read any type of data such as integer, character and float
Syntax of scanf
scanf (control_string, arg1, arg2.., argn);
Control_string
Format: %wp
%- conversion character, w-width of input value(optional), p-data type of
character
Control string may be continuous or separated by blank space, tabs etc. If this
character used, they are read by computer but will be ignored.

scanf

Some invalid input statements

General output (printf function)


Used to print any type of data such as numeric, single
character and string
General form:
printf(control_string, arg1,arg2 argn);
It may be noted that the arguments of printf do not
represent memory address and hence no ampersand(&)
sign required

Example printf

Specifier input single character (%c)


Reads single character

Input %s
Used to read string
General form: %ws(w represent length of the string)
This specification terminates reading at the counter of a blank space or new line

Input: %d
Read integer of data
Format; %wd (w is field width)
Can used separator
E.g: : ,
Assignment suppression
%*p
Exp: scanf(%d %*d %d,&a,&b);
Input: 100 200 300, the values of a and b are 100 and 300 respectively. 200 will not be
assigned to any value due to %*d. Useful when data is written in tabular form to skip a
particular column

Input: %f
This format spesification is used to read floating point of data
General form: %wf (w width of data)
Input: 12.3242.423
a=12.300000
b=242.000000
C=0.400000

Reading mixed data

Format specifier output (%d)


%wd- w width of integer

Output; %f
Width and precision can also be specified dynamically
printf(%*.*f,width,precision, arg);

Exercise
Write a program to find slope and midpoint of straight line whose
two end points are (x1,y1) and (x2,y2)
Write a program to find area of triangle whose coordinates of
vertices are (x1,y1), (x2,y2), (x3,y3)

Potrebbero piacerti anche