Managing Input and Output Operations
Managing Input and Output Operations
Managing Input and Output Operations
printf() getch()
scanf() getche()
getchar()
gets()
putch()
putchar()
puts()
Unformatted Functions:
They are used when I/P & O/P is not required in a specific format.
C has 3 types I/O functions.
a) Character I/O
b) String I/O
c) File I/O
a) Character I/O:
1. getchar() This function reads a single character data from the standard input.
(E.g. Keyboard)
Syntax :
variable_name=getchar();
eg:
char c;
c=getchar();
Like getch(), getche() also accepts only single character, but getche() displays the
entered character in the screen.
Syntax
variable_name = getche();
4 putch(): putch displays any alphanumeric characters to the standard output device.
It displays only one character at a time.
Syntax
putch(variable_name);
b) String I/O
1.gets() – This function is used for accepting any string through stdin (keyboard)
until enter key is pressed.
Syntax
gets(variable_name);
Example Program
void main()
{
char ch[30];
clrscr();
printf(“Enter the String : “);
gets(ch);
puts(“\n Entered String : %s”,ch);
puts(ch);
}
Output:
Enter the String : WELCOME
Entered String : WELCOME
Width Modifier: It specifies the total number of characters used to display the value.
Precision: It specifies the number of characters used after the decimal point.
E.g: printf(“ Number=%7.2\n”,5.4321);
Width=7
Precesion=2
Output: Number = 5.43(3 spaces added in front of 5)
Flag: It is used to specify one or more print modifications.
Flag Meaning
- Left justify the display
+ Display +Ve or –Ve sign of value
Space Display space if there is no sign
0 Pad with leading 0s
E.g: printf(“Number=%07.2\n”,5.4321)
Output: Number=0005.43
#include <stdio.h>
void main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("Sum: %d",sum);
}
Output
Enter two integers: 12 11
Sum: 23