why (getchar() != EOF) always equal to 1

A

arnuld

#include <stdio.h>

int main() {

printf("%d\n", getchar() != EOF);

return 0;
}

when i run this programme, no matter what i give it as input, output
is always equal to 1, never 0 (zero) or 5

why ?
 
M

Mike Wahler

arnuld said:
#include <stdio.h>

int main() {

printf("%d\n", getchar() != EOF);

return 0;
}

when i run this programme, no matter what i give it as input, output
is always equal to 1, never 0 (zero) or 5

Did you supply whatever your console defines as end-of-file?

For example, Microsoft Windows uses ctrl-Z, UNIX uses ctrl-D,
etc.


You've probably not sent the proper keystroke(s) for your
operating system. Consult your documentation.

-Mike
 
I

Ian Collins

arnuld said:
#include <stdio.h>

int main() {

printf("%d\n", getchar() != EOF);

return 0;
}

when i run this programme, no matter what i give it as input, output
is always equal to 1, never 0 (zero) or 5
Well if you type whatever the end of file character on you system is
(ctrl-D on Unix like systems) it will be 0.

A boolean expression can only be 0 or 1. Where did you get 5 from?
 
A

arnuld

Well if you type whatever the end of file character on you system is
(ctrl-D on Unix like systems) it will be 0.


ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
output is still "1"
but if i type "Ctrl-D" then output is "0" (zero).

A boolean expression can only be 0 or 1. Where did you get 5 from?

i was confused actually.

EOF = -1

on my system (Arch Linux i686, gcc 4.1.2)
 
D

dwks

ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
output is still "1"
but if i type "Ctrl-D" then output is "0" (zero).


i was confused actually.

EOF = -1

on my system (Arch Linux i686, gcc 4.1.2)

Look at your code.
#include <stdio.h>

int main() {

printf("%d\n", getchar() != EOF);

return 0;

}
You're printing a boolean value, like "1 == 0", which can be either
true or false. Most compilers use 1 for true and nearly all use 0 for
false. So that's what you get printed.

If you did this, on the other hand:

#include <stdio.h>

int main(void) {
printf("%d\n", getchar());
}

(leaving out the !=EOF), you would get (for an ASCII system) 48 if you
typed '0', 65 if you typed 'a', etc; the ASCII code for whatever
character you typed.
 
K

Keith Thompson

arnuld said:
ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
output is still "1"
but if i type "Ctrl-D" then output is "0" (zero).

No, the EOF character is not "-1".

EOF is (a macro that expands to) an integer value; it's not a
character. The getchar() function returns this value when it reaches
the end of the input file, or on an error; otherwise, it returns the
integer value of the input character. getchar() does *not* convert
strings of decimal digits to their numeric value; there are other
functions that will do that.

Actually, a boolean expression is either zero or non-zero -- but the
result of a relational or equality operator is always 0 or 1.
i was confused actually.

EOF = -1

on my system (Arch Linux i686, gcc 4.1.2)

BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
get "1" when i give "-1" as input ?

When you give "-1" as input, you're entering a '-' character followed
by a '1' character. If your system uses the ASCII character set, the
'-' character will cause getchar() to return the value 45; the '1'
character will cause it to return the value 49. Other character sets
will result in other values.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
K

Keith Thompson

dwks said:
Look at your code.
You're printing a boolean value, like "1 == 0", which can be either
true or false. Most compilers use 1 for true and nearly all use 0 for
false. So that's what you get printed.
[...]

Most? Nearly?

When an integer expression is used as a condition, the value 0 is
treated as false, and any non-zero value is treated as true.

The result of any relational ("<", "<=", ">", ">=") or equality ("==",
"!=") operator is *always* 0 for false, 1 for true.

Any compiler that gets this wrong is broken. (I've never heard of a C
compiler that was broken in that particular way.)
 
F

Flash Gordon

arnuld wrote, On 08/03/07 06:48:
ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
output is still "1"
but if i type "Ctrl-D" then output is "0" (zero).

BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
get "1" when i give "-1" as input ?

getchar gets a character. How many characters did you type when you
typed "-1"? Was any of those characters a signal that the end of file
has been reached?

It would make as much sense to ask why you email client did not close
its "compose" window when you typed in -1.

Now go and read the comp.lang.c FAQ at http://c-faq.com/
Question 12.1b specifically deals with your problem. Please always check
the FAQ before posting, it has answers to a lot of the easy questions so
you will get your answer a lot faster as well as avoiding the same
questions being asked over and over again. If you don't understand
something in the FAQ or cannot find the answer then by all means ask for
clarification, but in this case it is so obviously this question the
only way you could have missed it is to have not looked.
 
S

santosh

arnuld said:
ok. i use Linux, EOF character is an integer "-1".

The C standard does not specify the value of the macro EOF, just that
it expands to a value of type int that is the implementation's choice
for specifying end-of-file conditions. It's commonly -1, but that's
not a strictly portable assumption.
if i enter "-1"
output is still "1"
but if i type "Ctrl-D" then output is "0" (zero).

All input is treated as a sequence of characters. The character
sequence "-1" expands to the value specified in the system's character
set for '-' followed by that for '1'. Otherwise you will not be able
to enter "-1" when you want to do so. Signalling end-of-file, among
others is a control sequence. It's not meant to interfere with the
values assigned for normal input, hence special key sequences are
assigned to generate such values. Under UNIX systems it's commonly
generated by the key sequence 'Ctrl-d', (hold down the Control key and
press 'd'). Under Windows it's 'Ctrl-z'
BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
get "1" when i give "-1" as input ?

That's because to specify it from the keyboard, special key sequences
are needed to prevent it from interfering with normal input values.
Similarly, the system's end-of-line can be generated by Ctrl-m under
Unices.
 
M

Mark McIntyre

ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
output is still "1"

You're confusing the EOF flag with the EOF signal.
but if i type "Ctrl-D" then output is "0" (zero).

Crtl-D sends an end-of-file signal to your application. This causes
some functions to return a magic value which is typically -1.
BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
get "1" when i give "-1" as input ?

Because you told it to print the result of
"getchar is not equal to EOF"

which will be 1 (true) when getchar returned EOF, and zero otherwise.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

The C standard does not specify the value of the macro EOF, just that
it expands to a value of type int that is the implementation's choice
for specifying end-of-file conditions.

And must be negative (7.19.1 p3)

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Mark McIntyre said:
You're confusing the EOF flag with the EOF signal.


Crtl-D sends an end-of-file signal to your application. This causes
some functions to return a magic value which is typically -1.

I presume you're using "signal" in its colloquial English sense.
Since C does have its own concept of "signals" (see <signal.h>, C99
7.14), it's important to note that an end-of-file condition does not
normally cause a signal (SIGWHATEVER) to be delivered to the program.
It does "signal" the program by causing getchar() to return the value
of the EOF macro.
[...]
 
M

Mark McIntyre

I presume you're using "signal" in its colloquial English sense.

correct, I doubt anyone was confused but for clarity, I am indeed not
referrring to things found in signal.h
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top