473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

#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
12 4688

"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
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
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
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
"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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Dave | last post by:
Below is the code ive written just to count the characters typed in. I assumed EOF is -1, so if i type -1 and then press enter shouldnt the program end? It orks if i put something like 'q' in the...
11
by: Mazen S. Alzogbi | last post by:
Hi, I am a C beginner and I am trying to execute the following code from K&R book: main() { double nc; for(nc =0; getchar() != EOF; ++nc); printf("%0.f\n", nc);
6
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always...
10
by: Kobu | last post by:
My question is about EOF (which is (int)-1 on the implementation I use). Type char on my implementation is 'signed' too. How is it that the input library functions that return EOF can be used...
9
by: Sunner Sun | last post by:
Hi, all FAQ of comp.lang.c said "In C, type char is defined as occupying one byte, so it is usually 8 bits". (http://www.eskimo.com/~scs/cclass/notes/sx2a.html) But I found in C99 that "An...
13
by: broeisi | last post by:
Hello, Can someone help me out with this one? The following program should read characters till it met the EOF (-1) on my pc. I'm running linux and using the gcc compiler version 3.4.5. But...
8
by: rCs | last post by:
Which of the following two approaches is preferred (and why)? int c; do { ... c = getchar(); } while (c != EOF); - or -
20
by: Senthil-Raja | last post by:
The getchar() function is expected to fetch the next character in the input stream and return it. But, when I wrote a program using this function, it looks like the reading of the input stream...
5
by: amit1702 | last post by:
Hello everyone.. I am doing C Programming in Ubuntu G++ compiler in one program i need to come out out the code #include<stdio.h> char c; while( (c=getchar())!= EOF)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.