C Program Using Getchar And Putchar
Previous: Up: Next: Previous Page: Next Page: We have already seen how to read and print characters using our usual I/O built in functions, scanf and printf; i.e. The%c conversion specifier. We have also included the header file stdio.h in all our programs, because it contains the definition for EOF, and declares prototypes for these formatted I/O routines.
In addition, stdio.h contains two other useful routines, getchar and putchar, which are simpler to use than the formatted routines for character I/O. We use the term routine for getchar and putchar because they are actually macros defined in stdio.h which use more general functions available in the standard library. (Often routines that are macros are loosely referred to as functions since their use in a program can appear like a function call, so we will usually refer to getchar and putchar as functions).
Getchar In C Programming
The function getchar reads a single character from the standard input and returns the character value as the value of the function, but to accommodate a possible negative value for EOF, the type of the value returned is int. (Recall, EOF may be either 0 or -1 depending on implementation).
The simplest type of input and output in C programming takes place at the character level: One character goes in; one character comes out. The code in It Eats Characters reads a character from standard input by using the getchar() function at Line 8. The evil twin of the getchar() function is the putchar() function.
So we could use getchar to read a character and assign the returned value to an integer variable: int c; c = getchar; If, after executing this statement, c equals EOF, we have reached the end of the input file; otherwise, c is the ASCII value of the next character in the input stream. While int type can be used to store the ASCII value of a character, programs can become confusing to read - we expect that the int data type is used for numeric integer data and that char data type is used for character data. The problem is that char type, depending on implementation, may or may not allow negative values. To resolve this, C allows us to explicitly declare a signed char data type for a variable, which can store negative values as well as positive ASCII values: signed char c; c = getchar; An explicit signed char variable ensures that a character is stored in a character type object while allowing a possible negative value for EOF. The keyword signed is called a type qualifier. A similar routine for character output is putchar, which outputs its argument as a character to the standard output. Thus, putchar(c); outputs the ASCII character whose value is in c to the standard output.
The argument of putchar is expected to be an integer; however, the variable c may be either char type or int type (ASCII value) since the value of a char type is really an integer ASCII value. Since both getchar and putchar are macros defined in stdio.h, any program that uses these functions must include the stdio.h header file in the program. Let us rewrite our copy program using these new character I/O routines instead of using scanf and printf. The new code is shown in Figure. Characters are read until getchar returns EOF.
Each character read is printed using putchar. Sample output is shown below.File Copy Program. Type text, EOF to quit. Download cyberpunk 2020 home of the brave pdf free. This is a test. This is a test. Now is the time for all good men. Now is the time for all good men.
to come to the aid of their country. to come to the aid of their country. '136D The sample output shown here is for keyboard input so the effects of buffering the input is clearly seen: a line must be typed and entered before the characters become available in the input buffer for access by the program and then echoed to the screen.
Using getchar and putchar are simpler for character I/O because they do not require a format string as do scanf and printf. Also, scanf stores a data item in an object whose address is given by its argument, whereas getchar returns the value of the character read as its value. Both scanf and getchar return EOF as their value when they read an end of file marker in an input file. Previous: Up: Next: Previous Page: Next Page: tep@wiliki.eng.hawaii.edu Wed Aug 17 08:29:11 HST 1994.
Putchar Function C
USING GETCHAR AND PUTCHAR Only write a C program to perform simple C arithmetic calculations. The user is to enter a simple expression (integer operator integer) such as 15 + 30 The program is to extract the 2 operands and the operator, perform the indicated calculation and display the result.