M2 Input-Output Operations
M2 Input-Output Operations
Dr. Arunakumari B. N.
Assistant Professor,
Dept. of Computer Science and Engineering,
Email ID: [email protected]
getche()
gets() puts()
Formatted Input / Output Functions
Formatted Input / Output Functions classified into two types
1. scanf()
2. printf()
scanf():
› It is a formatted input function used to read one or multiple inputs from the
user.
› It reads input values from the user and stores in the specified variable address.
› Variable address list: each variable is prefixed with address operator(&) and
variables are separated by comma
Formatted Input / Output Functions Cont..
scanf():
› Examples:
‣ scanf(“%c”, &c);
‣ scanf(“%s”, s);
› The format specifier should match with the datatype of corresponding variables.
‣ int a; float x; char c; char s[12];
› Different delimiters (space comma etc ) can be used between different format
specifier then the input should also be separated by the same delimiters.
› Best practice is not use space any delimiter between format specifier.
printf():
› It is a formatted output function used to display one or multiple values on the
output screen to the user.
Examples:
› printf(“%s”, str);
Formatted Input / Output Functions
Examples:
› printf(“%s”, str);
› printf(“%d”, a+x);
› printf(“Hello”);
#include<stdio.h>
int main()
{
int sum, a=10, b=20;
sum = a + b;
printf(“sum=%d \n", sum);
}
Unformatted Input / Output Functions Cont..
The getchar() function reads one character from the user and store it in the specified
variable waits until user presses ENTER Key . Display the character on the screen.
#include <stdio.h>
int main()
{
char c;
printf("Enter a character : ");
c = getchar();
printf("\nEntered character : %c ", c);
}
Output:
Enter a character : y
Entered character : y
Unformatted Input / Output Functions Cont..
putchar() function display only one character on screen.
#include <stdio.h>
int main()
{
char c = 'K';
putchar(c);
}
Output:
K
Note: Here, variable c is assigned to a character 'K'. The variable c is
displayed by the putchar(). Use Single quotation mark ' ' for a character.
Unformatted Input / Output Functions Cont..
The getch() function reads single alphanumeric character input from the user and doesn’t
wait for the user to type ENTER key. But, that the entered character will not be displayed.
#include <stdio.h>
#include <conio.h> // console input output
int main()
{
printf("\nHello, press any alphanumeric character to exit ");
getch();
}
Output:
Hello, press any alphanumeric character to exit
Note:
The above program will run until you press one of many alphanumeric characters.
The key pressed by you will not be displayed.
Unformatted Input / Output Functions Cont..
The putch() function display single alphanumeric character on the screen.
Output:
Hello, press any alphanumeric character or symbol to exit
k
Note: The above program will run until you press one of many alphanumeric
characters. The key pressed by you will be echoed.
Unformatted Input / Output Functions Cont..
The gets() function can read a full string even blank spaces presents in a string.
The gets() function is used to get any string from the user.
Output:
Enter a string : bmsit college is awesome
bmsit college is awesome
Note: gets() reads input until it encounters newline or end of file, gets()does not stop
reading strings when it encounters white space instead it takes white space as a string
Unformatted Input / Output Functions Cont..
The puts() function display the character array or string on the screen.
Output:
Enter your Name: computer science
Computer science
Note: puts() function is similar to printf() function but we cannot print other
characters using puts() function
Formatting Output Functions
2 3
7 5 6
Formatting Output Functions
Formatting Output Functions
2 3 . 3 5 6 0
9 8 3 6 . 3 5 9
Formatting Output Functions
5 6 9 4 . 3 5 8 0
B M S I T C O L L E G E