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

Input and Output Statements - 1

The document discusses various input/output statements in C programming including getchar() and putchar() for single character I/O, gets() and puts() for string I/O, scanf() and printf() for general input and output of different data types, and format specifiers like %c, %s, %d, and %f for reading single characters, strings, integers, and floating point values respectively. Examples of using these statements with descriptions of their syntax and functionality are provided.

Uploaded by

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

Input and Output Statements - 1

The document discusses various input/output statements in C programming including getchar() and putchar() for single character I/O, gets() and puts() for string I/O, scanf() and printf() for general input and output of different data types, and format specifiers like %c, %s, %d, and %f for reading single characters, strings, integers, and floating point values respectively. Examples of using these statements with descriptions of their syntax and functionality are provided.

Uploaded by

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

Input and Output statements

Ts. Dr. Norfazillah Talib


JKP, FKMP
I/O single character
• getchar()
• putchar(variable)

Output
I/O of string
• gets( string)
• puts(string
General input(scanf)
• to read any type of data such as integer (%d), character
(%c) and float (%f)
• Syntax of scanf
– scanf(“control_string”, arg1, arg2….., argn);
– Each argument combination of ampersand(&)sign and variable
name
• 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 : abcde
output

output
Input %s
• Used to read string which is an array of characters
• General form: %ws(w represent length of the string)
• This specification terminates reading at the counter of a blank
space or new line
• the string name not requires any ampersand before it in scanf

Input: my name is..?

Input1 : Abu
Input2: bakar
I/O %c and %s
Input: in the waterfall

Input: all the best


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
• Separator scanf (“%c:%c:%c”) date, time
I/O %d
Input:123456789
Format specifier output (%d)
• %wd- w – width of integer
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
Format specifier output (%f)
• Format : %w.pf –w- width and p precision
I/O %f
Input: 12.3242.423
Output; %f
• Width and precision can also be specified
dynamically
• printf(“%*.*f”,width,precision, arg);
Reading mixed data
Input:
60
norfazillahtalib

You might also like