Introduction: Part 2 - Exploring Differences - CSU1051 - Shoolini U

Introduction

Differentiate between getchar(); and putchar()

getchar() putchar()
Definition Reads a single character from the standard input stream. Writes a single character to the standard output stream.
Input Reads input from the keyboard or a file. Writes output to the screen or a file.
Output Does not output anything. Outputs a single character to the screen or a file.
Return value Returns the ASCII value of the character read from input. Does not return anything.
Syntax int getchar(void) int putchar(int character)
Parameters Does not take any parameters. Takes a single parameter, which is the character to be written to output.
Looping Often used in a loop to read multiple characters from input. Often used in a loop to write multiple characters to output.
Buffering Uses buffering; characters are read into a buffer before being processed. Uses buffering; characters are written to a buffer before being displayed on the screen or written to a file.
Error handling Returns EOF (End of File) when it encounters an error or reaches the end of a file. Does not have any error handling mechanism.
Examples Read input from the keyboard: char c = getchar(); Output a single character to the screen: putchar('A');