Standard input-output functions:
There are 3 types of i/o functions in ‘C’ language.
1. Character-oriented I/O functions
2. Formatted I/O functions
3. String oriented I/O functions
Character-oriented I/O functions:
(a) getchar()
(b) putchar()
getchar(): This function is used to read a single character from the standard input device i.e.,
keyboard.
The general syntax of getchar() function is
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.
The general syntax of putchar() function is
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.
The general syntax of scanf() function is
scanf(“control string”, argument list);
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)
Rules for using scanf() function:
1. The control string must be placed in a pair of double quotes.
2. The variables in the argument list must be preceded by & (ampersand).
3. There should be a one to one mapping between the conversion specifications and the
address given in the argument list.
Examples:
scanf(“%d %c”, &n, &ch);
x scanf(“%d %c”, &n); no one-one mapping
x scanf(“%d %c”, &n1, &n2);
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);
x scanf(“%d %c %f”, &n, &m, &ch); ------ mismatch of types
printf(): This function is used to display any kind of data onto the standard input device.
The general syntax of printf() function is
printf(“control string”, argument list);
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.
Rules for using printf() function:
1. The control string must be placed in a pair of double quotes.
2. There should be a one to one mapping between the conversion specifications and the
address given in the argument list.
Examples:
scanf(“%d %c”, n, ch);
x scanf(“%d %c”, n); no one-one mapping
x scanf(“%d ”, n1, n2);
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);
x scanf(“%d %c %f”, n, m, ch); ------ mismatch of types
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
%f or %e or %g For floating point values
%h For short int
%ld For long integer values
%lf For double type values
%s For char.arrays or strings
%x For hexa decimal values
%o For octal values
%u For unsigned integers
%[ ] For strings with whitespaces
String oriented i/o functions:
(a) gets()
(b) puts()
gets(): This function is used to read a string from the standard input device i.e., keyboard.
The general syntax of gets() function is
getchar(stringvariable);
Example: char str[10];
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);
Example: char str[10]=”hello”;
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.
Escape Sequence Description
\n New line character
\t To provide tab space
\a To get Bell sound
\\ Single \
%% Single %
\b Backspace
\v Vertical tab
\f Form feed
\r Carriage return
\” Double quotes
\’ Single quote
\\ Back slash
\? Question mark
\0 null