0

What is the value compared in the following code?

while ((c = getchar()) != EOF)
        if ( c == '\n')

I know that '\n' is a constant variable because of the single quote. I know that it represents the numerical value of the character on the ASCII table, right? That is equal to 110. But what does

((c = getchar()) != EOF) return?

Thanks

MNY
  • 1,506
  • 5
  • 21
  • 34

3 Answers3

5

The crucial point is that c must be an int:

int c;

while ((c = getchar()) != EOF) { char read_value = c; /* ... */ }

It is assumed that an int can hold more values than a char, or at least more values that the system's narrow multibyte encoding uses*, and getchar returns a special constant EOF when there it failed to read more data. Otherwise, it is guaranteed that you can convert c to a char and obtain the value of the character that was read.

It is a common mistake to declare c itself as a char, in which case the loop might never terminate, since you might not be able to capture the special value EOF, or otherwise there would be a perfectly valid character which would be indistinguishable from (char)EOF.

*) For example, it would be perfectly fine if both a char and an int were 32 bits wide on a given platform, as long as, say, the narrow stream could only return units with values in the range [-128, 128), and you could use -200 as EOF.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • thank you for the great answer @Kerrek ! so if EOF input is not EOF it will return an int, and then if '\n' is an int too the statement will executes? i really wanted just to know that...if the only thing that will make the statement to executes is if both are integers..thanks allot – MNY Jan 09 '13 at 23:01
  • @user1959174: It is guaranteed that an `int` can hold all the values that a `char` can hold, and a `char` can hold all possible characters that are available on your system, including newline characters. – Kerrek SB Jan 09 '13 at 23:10
  • Got it! thanks for the patience :) @Kerrek, im sure it can be annoying to deal with newbies questions – MNY Jan 09 '13 at 23:12
  • 1
    @user1959174: It can actually be a lot of fun, since it forces you to make sure you understand what's going on in order to explain it. It's a great way to build one's confidence :-) – Kerrek SB Jan 09 '13 at 23:21
2

In c an assignment operation returns the rvalue which is the value on the right side of the equal sign. So in this instance c=getchar() returns whatever the character was or EOF.

ajon
  • 7,868
  • 11
  • 48
  • 86
  • Not quite. The value of an assignment operation is the value that was assigned. This can differ from the value on the right side of the equal sign, because the value on the right is converted to the type of the lvalue on the left, and this conversion can change the value. – Eric Postpischil Jan 09 '13 at 23:30
1

Your question was:

But what is: ((c = getchar()) != EOF) returens ?

The expression ((c = getchar()) != EOF) is a logical (or "Boolean") expression - it is either "true" or "false". Specifically, it tests to see whether something is not equal to the constant "EOF".

The "something" is the expression (c = getchar()). This is an assignment; it calls the getchar() function and assigns the return value to the variable c. The value of an assignment is the value assigned; therefore, the expression as a whole is causing the following to happen:

  1. getchar() is called
  2. Its return value is assigned to c
  3. That value is then compared to the constant EOF
  4. If the value is not equal to EOF then the while statement continues to operate.
prprcupofcoffee
  • 2,950
  • 16
  • 20
  • thanks @David, but can you tell me what value gets compared to '\n'? i understand the while statement, and trying to understand what value is getting compared in ( c == '\n') – MNY Jan 09 '13 at 23:08