C language Input and output can be classified into 2 types:
1. Formatted I/O
a. printf ()
b. scanf()
2. Un-Formatted I/O
a. getch()
b. putch()
c. getchar()
d. putchar()
e. gets()
f. puts()
3. Escape sequence
4. Format Specifiers
1. Formatted I/O:
Formatted functions allow us to display the results according to our requirements. The formatted functions return a value after executing the results. The return value will be equal to the number of variables successfully written or read from the input.
As formatted functions work with various data types they require format specifiers to understand the type of the variable they are working with.
printf()
“printf()” is used to display the output to the console. When “printf” is used, it will return number of bytes it has written to the console.
As “printf” can be used to display output of different types of variables, we need to include a format specifier. List of format specifer have been given at the end of the tutorial.
Example of a “printf()” statement displaying different types of variables.
#include<stdio.h>
int main()
{
int iNum = 1;
float fNum = 12.34;
char c = 'a';
int bytes_written = printf("%d %f %c \n", iNum, fNum, c );
printf("The number of bytes written on console by previous printf statement = %d \n", bytes_written);
}
Output:
1 12.340000 a
The number of bytes written on console by previous printf statement = 17
Width specifiers in printf():
“.<number>” – This is used in floating numbers. It tells the number of digits to be displayed after decimal point.
“%<number>d” – This is used for integer numbers.
Example:
#include<stdio.h>
int main()
{
int iNum = 12;
float fNum = 123.456789;
printf("%3d\n", iNum);
printf("%4d\n", iNum);
printf("%*d\n", 3, iNum);
printf("%*d\n", 4, iNum);
printf("%.2f\n", fNum);
printf("%.3f\n", fNum);
printf("%.4f\n", fNum);
}
Output:
12
12
12
12
123.46
123.457
123.4568
scanf():
scanf function is used to accept input of different data types. Hence format specifiers are important. This function will return the number of variables read from the console.
General syntax:
scanf(“format_specifier”, &<variable_name>);
Here we give the address “&” symbol, the compiler will go to that address and save the input value.
Example of scanf:
#include<stdio.h>
int main()
{
int iNum = 0;
float fNum = 0;
printf("Enter a int and float numbers\n");
int read_bytes = scanf("%d %f", &iNum, &fNum);
printf("The number of variable read from console is = %d \nThe value read is = %d and %f\n",read_bytes, iNum, fNum );
}
Output:
Enter a int and float numbers
12
12.34
The number of variable read from console is = 2
The value read is = 12 and 12.340000
2. Unformatted I/O:
Unformatted functions, we will not be having any control of how the data can be modified. They are just printed from starting of the line.
Unformatted functions will treat all the variables as a character data type. Hence format specifier is not necessary.
a. getch() and putch()
getch() accepts input from the console but doesn’t displays it.
getche() accepts input from console and displays it.
putch() displays a character to the console.
All the above 3 functions will work on one character at a time.
Example:
#include<stdio.h>
#include<string.h>
int main()
{
char ch;
printf("Enter a character\n");
ch = getch();
printf("\nEntered character is = \n");
putch(ch);
}
Note: Most of the modern compilers will not support above 3 functions.
b. getchar() and putchar()
Both of the above functions are used to accept a single character and display a single character respectively.
#include<stdio.h>
#include<string.h>
int main()
{
char ch;
printf("Enter a character\n");
ch = getchar();
printf("\nEntered character is = \n");
putchar(ch);
}
Output:
Enter a character
d
Entered character is =
d
c. gets() and puts()
gets() and puts() functions are used to read string from input and display the string to output respectively.
Example:
#include<stdio.h>
#include<string.h>
int main()
{
char string[20];
printf("Enter a string\n");
gets(string);
printf("\nEntered string is = \n");
puts(string);
}
Output:
Enter a string
warning: this program uses gets(), which is unsafe.
hello
Entered string is =
hello
3. Escape sequence
Escape sequence is used to give a special meaning. Usually they will be accompanied by a backslash and another character.
Below are the escape sequence available in C:
\n New Line
\\ Backslash
\b Backspace
\’ Single quote
\t Horizontal Tab
\” Double quote
\0 Null
\? Question Mark
4. Format Specifiers
As we saw in “Formatted I/O” we need to specify the correct format for a particular data type to work. Below are the different format specifiers that will be used in C language.
Short Int %d
Short Unsigned Int %u
Long Int %ld
Long Unsigned %lu
Floating %f
Double Floating %lf
Signed Character %c
Un Signed Character %c
String %s
Pointer address %p
Octal Number %o