0% found this document useful (0 votes)
8 views19 pages

Chapter 3 Output and Input

Uploaded by

Farhana
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)
8 views19 pages

Chapter 3 Output and Input

Uploaded by

Farhana
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/ 19

Input and Output

statements
Dr HO FU HAW
JKPI FKMP
I/O single character
• getchar()
• putchar(variable)
e.g. insert a character and
display a character
I/O of string
• gets( string)
• puts(string)
e.g. insert string that
less than 30
characters.
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 , e.g. %3s
• %- 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
Format specifier output (%d)
• %wd- w – width of integer
Format specifier output (%f)
• Format : %w.pf –w- width and p precision
Specifier input single character (%c)
• Reads single character
Example %c with input and output
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: my name is..?

Input1 : Abu
Input2: bakar
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
Example: %d and %f
• Input : 123.456
• First %2d is used to store integer in variable c
• Next, %3f is used to store 3 points float value in
variable j.
• Display the output on %d: 12 and %.2f: 3.40
Output; %*.*f
• Width and precision can also be specified dynamically
• printf(“%*.*f ”,width,precision, arg);
Thank you.

You might also like