By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

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

P: n/a
#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 ?

Mar 8 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
#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.

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

-Mike
Mar 8 '07 #2

P: n/a
arnuld wrote:
#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?

--
Ian Collins.
Mar 8 '07 #3

P: n/a
On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:

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)

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

Mar 8 '07 #4

P: n/a
On Mar 7, 11:48 pm, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:
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)

BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
get "1" when i give "-1" as input ?
Ian Collins.
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.

Mar 8 '07 #5

P: n/a
"arnuld" <ge*********@gmail.comwrites:
>On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:
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).
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.
>A boolean expression can only be 0 or 1. Where did you get 5 from?
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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #6

P: n/a
"dwks" <ki**********@primus.cawrites:
On Mar 7, 11:48 pm, "arnuld" <geek.arn...@gmail.comwrote:
[...]
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.
[...]

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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #7

P: n/a
arnuld wrote, On 08/03/07 06:48:
>On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:
>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?
<snip>
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.
--
Flash Gordon
Mar 8 '07 #8

P: n/a
arnuld wrote:
On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:

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".
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.

Mar 8 '07 #9

P: n/a
On 7 Mar 2007 22:48:23 -0800, in comp.lang.c , "arnuld"
<ge*********@gmail.comwrote:
>On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:

>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"
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
Mar 8 '07 #10

P: n/a
On 8 Mar 2007 03:23:50 -0800, in comp.lang.c , "santosh"
<sa*********@gmail.comwrote:
>arnuld wrote:
On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:

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".

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
Mar 8 '07 #11

P: n/a
Mark McIntyre <ma**********@spamcop.netwrites:
On 7 Mar 2007 22:48:23 -0800, in comp.lang.c , "arnuld"
<ge*********@gmail.comwrote:
>>On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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"

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.
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.
[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #12

P: n/a
On Thu, 08 Mar 2007 16:08:42 -0800, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
>On 7 Mar 2007 22:48:23 -0800, in comp.lang.c , "arnuld"
<ge*********@gmail.comwrote:
>>>On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:
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.
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
Mar 9 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.