Getchar() function in C
0 264
The getchar() function in C retrieves a character from standard input, typically the keyboard.
It reads the next character from the input buffer and returns its ASCII value as an integer.
This function is commonly used in programs where character-by-character input is required, such as reading user input one character at a time.
Steps:
1Include Header File:
Include the stdio.h header file to use getchar().
2Call getchar():
Use getchar() function to read a character from standard input.
3Store or Process:
Store the returned character or process it as needed.
Diagram:
Here's a simple diagram illustrating the usage of getchar():
syntax of getchar() in c:
int getchar(void);
getchar(): This is the function name used to read a single character from the standard input.
void: The getchar() function does not take any arguments.
int: The function returns an integer, which is the ASCII value of the character read from the standard input.
Example:
// Program for getchar() in C #include<stdio.h>Output:int main() { char ch; printf("Enter a character: "); // Using getchar() to read a character ch = getchar(); printf("You entered: %c\n", ch); return 0; }
Input: User enters 'A' Enter a character: A Output: You entered: A Input: User enters '5' Output: Enter a character: 5 You entered: 5
In this program, getchar() reads a single character from the standard input, which is then stored in the variable ch.
The entered character is then displayed back to the user using printf().
Share:
Comments
Waiting for your comments