C প্রোগ্রামিংয়ের getchar() Function এর ব্যবহার

Submitted by tushar pramanick on Tue, 03/05/2013 - 15:05

Using the getchar() Function
The C language provides another function, getchar(), to perform a similar operation to getc(). More precisely, the getchar() function is equivalent to getc(stdin).

 

The syntax for the getchar() function is

 

#include <stdio.h>
int getchar(void);

 

Here void indicates that no argument is needed for calling the function. The function returns the numeric value of the character read. If an end-of-file or error occurs, the function returns EOF.

 

The program in Listing 5.2 demonstrates how to use the getchar() function to read the input from the user.

 

TYPE
Listing 5.2. Reading in a character by calling getchar().

 

1:  /* 05L02.c: Reading input by calling getchar() */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int ch1, ch2;
7:
8:     printf("Please type in two characters together:\n");
9:     ch1 = getc( stdin );
10:    ch2 = getchar( );
11:    printf("The first character you just entered is: %c\n", ch1);
12:    printf("The second character you just entered is: %c\n", ch2);
13:    return 0;
14: }

 

OUTPUT
 After running the executable file, 05L02.exe, and entering two characters (H and i) together without spaces, I press the Enter key and the following output is displayed on the screen:

 

C:\app> 05L02

 

Please type in two characters together:
 

Hi

 

The first character you just entered is: H

 

The second character you just entered is: i

 

C:\app>

 

ANALYSIS
The program in Listing 5.2 is quite similar to the one in Listing 5.1, except that the former reads in two characters.

 

The statement in line 6 declares two integers, ch1 and ch2. Line 8 displays a message asking the user to enter two characters together.

 

Then, the getc() and getchar() functions are called in lines 9 and 10, respectively, to read in two characters entered by the user. Note that in line 10, nothing is passed to the getchar() function. This is because, as mentioned earlier, getchar() has its default file stream–stdin. You can replace the getchar() function in line 10 with getc(stdin), because getc(stdin) is equivalent to getchar().

 

Lines 11 and 12 send two characters (kept by ch1 and ch2, respectively) to the screen.

Comments

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?