Check The Programming Section
Unformatted input output functions work only with character data type. These functions do not require any format specifier like printf() and scanf(), because they work with only character data type. If any other data value is passed during the input, it will be treated as character only. These unformatted I/O functions are defined in stdio and stdlib header files. Unformatted I/O functions are categorised into three groups as follows:
Character I/O
String I/O and
File I/O
getchar()
This function reads one character value from standard input and it reads only one character, till the user press enter key. The syntax of the function is as follows:
int getchar(void)
No argument is required and it accepts one unsigned character as input and returns it as int as its ASCII value after conversion. The return value can write to a char type variable using an assignment operator. For example,
#include<stdio.h>
int main(){
char ch;
ch = getchar();//line2
printf("The given character ch = %c", ch);
}
During the execution of the code, the console will wait for an input. After entering any value, its ASCII value will be returned by the getchar() function. Before assigning this value to ch at line2 the ASCII value gets converted to char, due to the assignment operator and assigns it to ch. We can also use the getchar() function as initialization of the char variable. For example,
char ch = getchar();
The value entered by the keyboard will be assigned to ch as its initialization value.
putchar()
This function did the opposite task of getchar(), it prints one character on the screen at a time. The prototype of the function is as follows:
int putchar(int ch);
Argument Type
ch - This function takes an integer value as input and displays its equivalent character. If input is given as char type then no conversion is required and displayed. If input is given as int, then it is considered as ASCII value and converts it and displays it as char value.
Return Type
int - putchar( ) returns an integer after writing the unsigned char and cast to int.
#include<stdio.h>
int main(){
char ch = getchar();//a character is given as input and assigned it to ch
int k = putchar(ch);//prints the value of ch
ptintf("%d",k);//prints the ASCII value of ch.
}
getche()
This function is defined in the conio.h header file in C/C++, used for console input and output. The getche() function waits to press any key from the keyboard and displays the pressed value and returns as integer. The prototype of the function is as follows:
int getche(void);
It does not have any argument so considered as void means null. Returns an int as the ASCII value of the character pressed from the keyboard. For example,
#include<stdio.h>
#include<conio.h>
int main(){
printf("Press any key ::");
char ch = getche();//line2
printf("\nThe entered character is %c",ch);
}
After displaying the message Press any key:: waits to press any key from keyboard at line2. Once a key is pressed, it displays to the console and returns it to ch. At last the printf() statement at line3 gets executed and prints the value of the ch.
getch()
getch() is a nonstandard function defined in conio.h header file. This function is designed for MS-DOS system based C compiler Turbo C. This function is not part of the standard C library. The prototype of the function is as follows:
int getch(void);
Argument Type
This function does not require any argument.
Return Type
It returns a value as int after casting an unsigned char.
This function just waits to press any key and once the key is pressed the key value is not displayed on the screen and returns the character as int. For example,
#include<conio.h>
#include<stdio.h>
int main(){
char ch = getch();//line1
printf("%c", ch);//line2
}
The screen is halted due to the function getch() at line1 and waits to press any key. The pressed key does not display to the screen, but the value is returned to ch and gets printed by the printf() statement of line2.
gets()
This function is used to read characters from the standard input (stdin) and saves it to a character array till encountering a new line character or the end-of-file is reached. The prototype of the function is as follows:
char * gets(char *str);
Argument Type
*str - it is a pointer to character array, in which the read characters from stdin will be saved and represented as a null terminated C string.
Return Type
char * - the gets function returns the str, which is used as an argument while taking the input.
Note: The problem with gets is that, it did not check the bound of str while taking the characters as input. So it can accommodate more numbers of characters than the capacity of str. For example,
#include<string.h> //for strlen()
#include<stdio.h>
int main(){
char str[10];
printf("Enter any string:: ");
gets(str);
printf("%s",str);
printf("\nThe number of characters given as input = %d",strlen(str));
}
The Output of the Code
Enter any string:: We are learning about unformatted Functions
We are learning about unformatted Functions
The number of characters given as input = 43
We have entered 43 characters, but str is declared with buffer size 10. So it is advised that we must ignore gets() to take input of any string.
fgets()
This function is the same as gets() to take input characters till the encounter of a null character or end-of-file. But, instead of taking input beyond the size of the buffer like gets(), it can take the number of characters as input till the size of the buffer. This function is better than gets() with respect to data security. The prototype of the function is as follows:
char * fgets(char *str, n, FILE *stream);
Argument Types
*str - pointer to a char array with fixed size and consider as buffer. The characters read from stdin or FILE *stream will be saved to str.
n - it represents the maximum number of characters to be saved to str, including the terminating characters. The value n less than or equal to size of str.
*stream - a pointer to FILE object represents the input stream. We can also use stdin as an argument to read characters from standard input.
Return Type
fgets() returns the *str, which is used as an argument.
Let’s give input more number of characters then the size of the character buffer and check how many characters are considered by fgets() as input.
The Source Code
#include<string.h> //for strlen()
#include<stdio.h>
int main(){
char str[10];
printf("Enter any string:: ");
fgets(str,10,stdin);//the value of n is 10
printf("%s",str);
printf("\nThe number of characters given as input = %d",strlen(str));
}
Output of the code
Enter any string:: I love to learn programming.
I love to
The number of characters given as input = 9
In the above source code value of n is taken as 10 equal to the size of the str. During input more number of characters than the size of str is given as input. But when printf() is used to print the str it prints “I love to” containing only 9 (n-1) characters only. Means fgets() always checks the size of the str before saving a character to the buffer.