Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CHAP – 5

MANAGING INPUT –
OUTPUT OPERATIONS

Prepared by:
Ms. Pankti Dharwa
(SVBIT, CE dept)
INPUT / OUTPUT WITH SCANF() /
PRINTF()
• SYNTAX:- Printf(“Controlstring”,list of
address variables)
• Control string

Normal character Escape sequence Format Specifier

It is used to display output on the standards output


device.
• SYNTAX:-
Scanf(“Controlstring”,&arg1)
It is used to read the variable values
from the keyboard. The values are
stored at respective memory
locations.
Int a;
scanf(“%d”,&a);
Char a;
scanf(“%c”,&a);
Float a;
scanf(“%f”,&a);
CHARACTER INPUT / OUTPUT
• getchar():-The getchar() used for getting
character input from keyboard.
• Syntax :- variable_name = getchar();
putchar() :-is used for displaying one character
on the screen.
Syntax:- putchar(variable_name);

• Ex:-main()
{
char c;
printf(“enter one character\t”);
c=getchar();
printf(“Input character is );
putchar(c);
}
Output:- Enter one character a
Input character is = a
• getch();
• getche();
• Ex:-main()
{
char c;
printf(“enter one character\t”);
c=getch(); //input not echoed
printf(“\n Input second character );
c1=getche(); //input echod
}
Output:- Enter one character //a
Input second character b
STRING INPUT / OUTPUT
(ch 8)
• Gets() and puts() are used to read a
string & print the string.
• gets(string_variable) puts(string_variable)
Ex:- char name[10]; puts(“hello”);
gets (name);

Gets() to read Puts() is used to


string type of display string on the
input from screen
keyboard.
FORMATTED INPUT / OUTPUT
• Formatted input using format specifier :-
%wd
W- width(number of digits), d-integer number.
EX:- scanf (“%3d %4d”,&num1,&num2)
if the input is given like 234 4563
Then num1 = 234 & num2 = 4563
But if input is give like 2344 563
Then num1 =234 & num2 = 4
• Scanf() tries to read 4 digit for num2,but
immediately after 4 it find space where it stops
reading.
–All in single program
• scanf(“%2d %4d”,&x,&y);
• printf(“x=%d y=%d”,x,y);
• i/p - 9789 4321
• o/p- x= 97 y= 89

• scanf(“%d %d”,&a,&b);
• printf(“a= %d b=%d”,a,b);
• i/p = 44 66
• o/p- a=4321 b=44

• scanf(“%3d %4d %3d”,&p,&q,&r); printf(“p=%d


q=%d r=%d”,p,q,r);
• i/p-123456789
• o/p:-p=66 q=1234 r=567
Solution…
• Scanf();
• Fflush(stdin);
• It will clear input buffer before next
scanf …
FORMATTED OUTPUT
• Formatted output means to print output in some
format.
• To print the integer in formatted manner we use
%wd width(number of digits), d-integer number.
• printf(“%d”,123); 1 2 3

• printf(“%5d”,123); 1 2 3
• printf(“%2d”,123); 1 2 3

• printf(“%05d”,123); 0 0 1 2 3

• printf(“%-5d”,123); 1 2 3

• printf(“%+5d”,123); + 1 2 3
printf(“%#5o”,123); 0 7 1 0
The format for floating data is %w.pf
• printf(“%f”,45.678); 4 5 . 6 7 8 0 0 0

• printf(“%7.2f”, 45.768); 4 5 . 6 8

• printf(“%-7.2f”,45.768); 4 5 . 6 8

• printf(“%7-.2e”,45.678); 4 . 5 7 e + 0 1

• printf(“%e”,45.678); 4 . 5 6 7 8 0 0 e + 0 1

You might also like