0% found this document useful (0 votes)
11 views37 pages

3 Inputandoutput

Uploaded by

muhdfaridmfd0
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)
11 views37 pages

3 Inputandoutput

Uploaded by

muhdfaridmfd0
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/ 37

Chapter 3:

Input and Output statements


Input and output (I/O):
‘character’

• getchar( )  request an input


• putchar(variable)  display the output
e.g. insert a character and
display a character
I/O of string (long characters)

• gets( string)  request string input


• puts(string)display string output
e.g. insert string data, e.g. a[30]:
‘a’ is the variable name, [30] refers to the total set
Of string data can be stored
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”  to save data into variable

Scientific notation

(same as float)
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 for integer
‘%d’

• %(w)d  w – width of integer/ format of integer


Reading multiple %d in scanf
• scanf(“%d”,&a)  read & store all digits
• scanf(“%2d”,&a)  read & store 2 digits into “a”
• scanf(“%4d”,&a)  read & store 4 digits into “a”
• scanf(“%2d%2d”,&a,&b);  read & store 2 digits into “a”, 2 digits into “b”
• scanf(“%2d%2d%3d”,&a,&a,&b);  read & store 2 digits into “a”, follow
by 2 digits into “a” again, follow by 3 digits into “b”
Example
Input data type (integer) : %d
• Read integer of data
• Format; %wd (w is field width)
• Can used separator
• E.g: ‘:’ ‘,’ ‘ ’
• Assignment suppression
• %*d
• 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
Exercise 1.0: Read %d data
Given the scenario:
#include<stdio.h>
int main( )
{ int x,y,z;
scanf(“%2d%2d%2d%2d”,&x,&x,&y,&y);
printf(“%d%d”,x,y);

return 0;
}
Exercise 2.0: Read %d data (integer)
Given the scenario:
#include<stdio.h>
int main( )
{ int x=2,y=3,z=4;
scanf(“%1d%2d%2d%2d”,&x,&y,&x,&y);
printf(“%05d%+5d”,x,y);

return 0;
}
Exercise

int a,b,c,
float A=‘A’;
scanf(“%1d%2d%3d%3d%2d%d”,&a, &b, &a, &b, &c);
printf(“%5d,%+4d, %06d, %-5d”, A, a ,b , c);
Given value: 123456789012345
Example %c input and output
Input single character data “%c”
Example %c input and output
Exercise

• Char x;
• scanf("%c%c%*3c%c",&x,&x,&x);
• printf("%c",x);

Input: universityuthm
Input %s (string)

• Used to read string (more than 1 character)


• 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
Exercise 5.0: Read %c data
#include<stdio.h>
int main( )
{char x[5], y, z[5];
scanf(“%2s%c%2c”,x, &y, z);
printf(“%s%c%s”, x, y, z);

return 0;}
Exercise 6.0: Read %c & %s data
#include<stdio.h>
int main( )
{ int a = 65;
char b, c;
char d[5];
scanf(“%c%c%3s%2s”,&b, &c, d, d);
printf(“%c%c%c%s”,a,b,c,d);
return 0;
}
Format specifier output for float (%f)
• Format : %w.pf –w- width and p precision
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 (read as %4f)
• b=242.000000 (read as %3f)
• C=0.400000 (read as %2f)
Exercise

• Scanf(“%2f%3f”,&x, &y)
• Input: 123.456.78.90

• Scanf(“%4f%2f%1f”,&x,&y)
• Input: 8.9032.21.23

Exercise 3.0: Read %f data
#include<stdio.h>
int main( )
{ float x,y,z;
scanf(“%2f%3f%2f”,&x ,&y, &z);
printf(“%.2f %06.1f %5.2f”,z, y, x);

return 0;}
Exercise

• Scanf(“%1f%2f%3f”,&x, &x, &y)


• Printf(“%05.2f, %4.1f”, &x, &y)
• Input: 123.456.78.90
Reading ‘blank space’ & “ * ”data
• Blank Spacing - will be counted as input data or considered as a stop point
• ‘ * ’ – refer to bypass or ‘jumped’

Scanf(“%2d%*3d%2d”,&a,&a);
Input: 1234 5678  read 12 by pass 34  read 56 Consider blank space

Input: 12345678  read 12 by pass 345 read 67


Exercise 7.0: Read ‘blank space’ & ‘ * ’ data
Given the scenario:
#include<stdio.h>
int main( )
{ int a,b,c=5;
scanf(“%2d%*2d%2d%2d”,&a,&a,&b);
printf(“%d%d%d”,a,b,c);

return 0;
}
Reading mixed data (%d and %c)
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
Exercise reading mixed data (%d and %f)
Reading ‘blank space’ & * data

• Blank Spacing - will be counted as input data or considered as a stop point


• ‘ * ’ – refer to bypass or ‘jumped’
• Applicable for integer (%d)
Exercise 7.0: Read ‘blank space’ & ‘*’ data
Given the scenario:
#include<stdio.h>
int main( )
{ int a,b,c=5;
scanf(“%2d%*2d%2d%2d”,&a,&a,&b);
printf(“%d%d%d”,a,b,c);

return 0;
}
*.*
Output; %*.*f number number
e.g. K=2 e.g. j=3

• Width and precision can also be specified


dynamically
• printf(“%*.*f”,width,precision, arg);
Exercise: %*.*f

• Given X=3.142323, Y=4, Z=2


• printf(“ the value of X is %*.*f”, Y, Z, X);
Thank you.

You might also like