#include <stdio.h>
#include <string.h>
void getOneChar()
{
int x;
printf("Enter Character: \n");
x = getchar();
printf("You Entered: \n");
putchar(x);
}
int main()
{
getOneChar();
return 0;
}
Result:
Enter Character:
c
You Entered:
c
Process returned 0 (0x0) execution time : 3.334 s
Press any key to continue.
This program defines a function called getOneChar
which prompts the user to enter a character and reads it from the standard input stream using getchar()
. Then, it prints the character to the standard output stream using putchar()
.
In the main()
function, the getOneChar()
function is called to execute the above functionality. The program ends after the function is executed.
getchar() and putchar() are two functions in C that are used for character input/output operations.
The getchar() function reads a single character from the input stream. The function reads the character from the input buffer and returns it as an integer value. This function waits until the user enters the character and hits enter or return key. It reads any character including white spaces and newlines. The syntax for getchar() is:
int getchar(void);
The putchar() function is used to output a single character to the output stream. It takes a single character as an argument and prints it to the console. The syntax for putchar() is:
int putchar(int ch);
The integer value returned by getchar() is the ASCII value of the character that is read from the input stream. This means that we can store the value in an integer variable or compare it to other character values. It is important to note that getchar() reads one character at a time, and if we want to read multiple characters, we need to call it multiple times.
Similarly, putchar() takes an integer value as an argument, which represents the ASCII value of the character to be printed. The function then prints the character to the output stream. The putchar() function does not add a newline character by default, so if you want to print a newline character, you must include it explicitly in the argument list.
Both getchar() and putchar() are part of the C standard library, and they are defined in the stdio.h header file. These functions are very useful when dealing with character data in C, and they are commonly used for reading user input and printing output to the console.
No comments:
Post a Comment