xwb1989
xwb1989

Reputation: 343

Extra loop when using getchar() and WHAT indeed getchar() behave

When I want to figure out what does getchar() actually do, this little piece of loop did confuse me.

int i;
int c;
for (i = 0; i < 100; i++) {
    c = getchar();
    printf("%d\n", c);    
    printf("i is %d\n", i);
}

The input and output is:

input: 1
output:
49
i is 0
10
i is 1

input: 12
output:
49
i is 2
50
i is 3
10
i is 4

As I previously supposed, if I enter 1 character, the getchar() should extract it out and putchar() would print it, then the program move to the next loop and wait for my next input. But the results seem to show that the code do not work as I supposed:

  1. What do the output numbers mean?
  2. There is always an extra loop printing 10, what does this 10 mean? If it means EOF, why after replacing c = getchar(); with c = (getchar() != EOF); within the loop, the code always print 1 which, as I supposed, should print a 0 in the last loop?

Thx very much!

Upvotes: 1

Views: 1079

Answers (2)

md5
md5

Reputation: 23699

Question

What do the output numbers mean?

The output numbers refer to the value of your characters according to your character set, usually based on ASCII (some mainframes use also EDCDIC).

C11 (n1570), § 5.2.1 Character sets

Two sets of characters and their associated collating sequences shall be defined: the set in which source files are written (the source character set), and the set interpreted in the execution environment (the execution character set). Each set is further divided into a basic character set, whose contents are given by this subclause, and a set of zero or more locale-specific members (which are not members of the basic character set) called extended characters. The combined set is also called the extended character set. The values of the members of the execution character set are implementation-defined.

Therefore, through this character encoding, 49 is the character'1' and 50 is the character '2'.

Question

There is always an extra loop printing 10, what does this 10 mean?

With ASCII charset, 10 is the linefeed character '\n'.

When you are typing the character '1' on your keyboard, the standard input stream stdin will receive in fact two characters : '1' and '\n', since you are pressing <Enter> to valide your input.

Therefore, you should clean the standard input stream once you have done your getchar call. One possible way to achieve it is to consume every characters until you reach a newline character or EOF:

#include <stdio.h>

int c;

while ((c = getchar()) != '\n' && c != EOF)
  ;

On BSD, there is also the function fpurge and, on Solaris and GNU/Linux, __fpurge is available.

Question

If it means EOF, why after replacing c = getchar(); with c = (getchar() != EOF); within the loop, the code always print 1 which, as I supposed, should print a 0 in the last loop?

The value of EOF can't be 10, since EOF must have a negative value.

C11 (n1570), § 7.21.1 Introduction

EOF, which expands to an integer constant expression, with type int and a negative value [...].

Upvotes: 2

user529758
user529758

Reputation:

What do the output numbers mean?

Character codes of the character getchar() returns, since you get a one and you're printing it using the %d specifier. In this case, it seems your character encoding is ASCII or maybe UTF-8, so 1 stands for 49, 2 for 50, etc.

2: [... too long to quote...]

10 is the ASCII and Unicode char code for newline ('\n'). Since you press Enter (getchar() waits for it!), you will get the character that Enter sent to the terminal.

Upvotes: 2

Related Questions