5.std io fun
5.std io fun
(a) getchar()
(b) putchar()
getchar(): This function is used to read a single character from the standard input device i.e.,
keyboard.
charactervariable=getchar();
Example: char ch;
printf(“Enter a character”);
ch=getchar();
putchar(): This function is used to display a single character on the standard input device i.e.,
monitor.
putchar(charactervariable);
Example: char ch;
printf(“Enter a character”);
ch=getchar();
putchar(ch);
Formatted i/o functions:
(a) scanf()
(b) printf()
scanf(): This function is used to read any kind of data from the standard input device.
Here, the control string contains the format specifications (%d %c etc) and the argument
list contains the addresses of variables that stores the actual data (&n, &ch)
4. The order and types of conversion specifications in the control string must match with the
order and types of variables used in the argument list.
Examples:
int n;
char ch;
float m;
scanf(“%d %c %f”, &n, &ch, &m);
printf(): This function is used to display any kind of data onto the standard input device.
The general syntax of printf() function is
Here, the control string contains the format specifications (%d %c etc) along with some
message (optional) and the argument list contains the set of variables to be displayed onto the
monitor.
5. The order and types of conversion specifications in the control string must match with the
order and types of variables used in the argument list.
Examples:
int n;
char ch;
float m;
scanf(“%d %c %f”, n, ch, m);
The list of format specifiers used in ‘c’ language are given below
Format code Data Type
%d or %i or %n For integers
%c For characters
(a) gets()
(b) puts()
gets(): This function is used to read a string from the standard input device i.e., keyboard.
getchar(stringvariable);
printf(“Enter a string”);
gets(str);
puts(): This function is used to display a string on the standard input device i.e., monitor.
The general syntax of puts() function is
putchar(stringvariable);
puts(str);
Escape sequences / back slash character constants:
Escape sequences in ‘c’ language are the character representations that may appear either in a
character constant or in a string constant.
An escape sequence always begins with a back slash (\) followed by one or more special
characters.
\\ Single \
%% Single %
\b Backspace
\v Vertical tab
\f Form feed
\r Carriage return
\” Double quotes
\’ Single quote
\\ Back slash
\? Question mark
\0 null