UnFormatted Input Statements:
These statements are primarily concerned with reading the character type data from the keyboard.
The getchar() and gets() Functions are used for this purpose. Since, they are included in the stdio.h Header File, the C Program that uses these functions should exclusively have the following preprocessor statement :
#include<stdio.h> |
a) getchar() Function:
This Function reads a single character from the standard input device. There is no parameter within the parentheses. Its syntax is:
char_var = getchar(); |
where
char_var is a character type variable to which an accepted character is assigned.
Example:
main()
{
char letter;
letter = getchar();
}
b) gets() Function:
This Function reads in everything you enter from the keyboard until the ENTER key or RETURN is pressed. Here everything means a string which is a sequence of all printable ASCII characters. The syantax is:
gets(string); |
where
string is a sequence of characters and is of type char.
Example:
UnFormatted output Statements:
a) putchar() Function:
where
Example:
Program:
b) puts() Function:
where
Example:
Program:
Example:
main()
{
char name[25];
printf(" Enter your Name\n");
gets(name);
}
UnFormatted output Statements:
These statements are mainly concerned with displaying or printing the character type data on the monitor(screen). The putchar() and puts() Functions are used for this purpose. They are included in the stdio.h Header File. So if we use unformatted statements in C Programs then
#include<stdio.h> Header File is required.
a) putchar() Function:
This Function prints a single character on the screen. The character to be displayed is of type char. Its syntax is:
putchar(ch_var); |
where
ch_var is a character variable which is enclosed within the parenthesis.
Example:
main()
{
char ch;
putchar(ch);
}
Program:
#include<stdio.h>
main()
{
char dhoni;
dhoni = getchar();
putchar(letter);
}
The following Program illustrates both reading and printing of a single character.
b) puts() Function:
This Function prints a string of characters on the screen. The syntax is:
puts(string); |
where
string is a sequence of characters.
Example:
#include<stdio.h>
main()
{
char msg[20];
puts(msg);
}
Program:
#include<stdio.h>
main()
{
char msg[20];
printf(" Enter the msg\n");
gets(msg);
puts(msg);
}
0 Response to "Overview of I/O Statements of C Language i.e puts() , gets() , getchar() and putchar() with Examples"
Post a Comment