In order to keep C Programming language compact, Dennis Ritchie removed anything related to the input or output from the definition of the language. Therefore, C has no provisions for input and output of data from input and output devices.
In order to solve this little discrepancy, the C developers developed several standard input and output functions and placed them in C libraries. All these libraries are accessible by all the C compilers.
Also, this implies that the way different operating systems use these I/O functions will differ for every operating system. The printf() function for a DOS-based compiler has been written keeping in mind the way a DOS-based compiler would display the output. There are required differences in the Windows and Unix based compilers as well. Thus, this feature is OS dependent.
Types of Input/Output functions
A lot of Input/output functions have been defined in the standard libraries. These can be classified as follows:
- Console I/O functions: These functions allow us to receive input from the input devices like keyboard and provide output to the output devices like the Visual Display Unit.
- File I/O functions: These functions allow us to access the hard disk or floppy disk to perform input and output.
Console I/O functions
A console comprises the VDS and the keyboard. The Console Input and Output functions can be classified into two categories:
- Formatted console I/O functions: These functions allow the user to format the input from the keyboard and the output displayed in the desired manner.
- Unformatted console I/O functions: These functions do not allow the user this feature.
These various formatted and unformatted console I/O functions are shown in the figure below:
Formatted Console Input Function: scanf()
scanf() is the formatted console input function which reads formatted input from stdin(standard input). It can read any integer, float, character, string, etc data from the user. The syntax of using scanf() is as follows:
scanf("format specifier" ,arguments address);
Let us consider an example: scanf(“%d”, &num);
In this example, the %d is the format specifier for an integer hence, num stores an integer value. &num indicates the address of num where the value is to be stored under the variable name ‘num’.
Note:- One disadvantage of scanf() when taking string inputs is that it will ignore the string that has been entered after a blank space.Thus, scanf() does not take multi-word string inputs.
To take multi word string inuts we should use gets() method.
Formatted Console Input Function: printf()
printf() is the formatted console output function which prints the formatted output to the stdout(standard output). It can display integers, floating point values, characters, string, etc as indicated by the user. The syntax of using printf() is as follows:
This shall simply print “text” on the output screen. Any other textual data can be written within double quotes and displayed using printf() function. In order to print any variable value along with text we have the following format:
printf("text <format specifier>",variable);
This shall print the value of the variable in the format specified by the format specifier.
For example, printf(“marks: %d”,marks); This has the format specifier as %d which stands for integer data and it shall print the marks in integer format.
Note: For floating point values the format specifier is %f and it shall print 6 decimal places after the floating point. For example, 3.6 will be displayed as 3.600000. In order to limit the decimal places after the floating point, the format specifier can be written as %0.3f for 3 decimal places after the floating point.
Unformatted Console I/O functions
The unformatted console input/output functions deal with a single character or a string of characters. Let us see how all these unformatted functions work:
|getch()||This function returns the character that was typed last or was typed most recently. It does not display(echo) the character on the screen. It is present in the header file <conio.h>.|
|getche()||This function is the same as getch(). The only difference is that it echoes(displays) the most recently used character. It is also present in <conio.h>.|
|getchar()||getchar() is a macro that works in a similar manner as getch() and also displays(echoes) the character but it needs the user to press the Enter key after the character. It is present in the header file <stdio.h>.|
|fgetchar()||fgetchar() works in the same manner as getchar() . But fgetchar() is a function.It is also present in <stdio.h>.|
|putch()||This function prints a single character on the console.|
|putchar()||This function works in the same way as putch().|
|fputchar()||This function works in the same way as putch().|
|gets()||It takes a string input(single-word or multi-word) from the user. It terminates when the Enter key is pressed.|
|puts()||It is used to print the string to the console.|
An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.
Do not forget to share and Subscribe.