473,834 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getchar() and EOF confusion

Mostly when I want to take input from stdin I use getchar() but I get this
from man page itself:

"If the integer value returned by getchar() is stored into a variable of
type char and then compared against the integer constant EOF, the
comparison may never succeed, because sign-extension of a variable of type
char on widening to integer is implementation-defined"
while( EOF != (ch = getchar()) ) ....
I use it like that. Can I run into problems with that ?


--
www.lispmachine.wordpress.com
my email is @ the above blog.

Oct 15 '08
22 3614
On Tue, 14 Oct 2008 23:39:59 -0700, Keith Thompson wrote:
Note that type char is an integer type. It's important to distinguish
between an integer type (of which there are several, including char,
int, unsigned long, etc.) and the specific integer type called "int".
The name "int" was obviously formed as an abbreviation of the word
"integer", but they mean different things.

Now I am much curious. Whats the different between an "integer" and a
variable of type "int". Do "integer types" are different from "int types"
..SNIP..
You're unlikely to run into this in practice. Machines with this
characteristic are typically DSPs (digital signal processors) which
typically have freestanding C implementations , so stdio.h might not
even be available. But if you want your code to be 100% portable, you
can first check whether the result returned by getchar() is equal to
EOF, and then check whether either feof() or ferror() returns a true
value. In practice, we don't generally bother.

Now I know why some clc lurker told me to distinguish between real end
of file (no more input) and the not so real end of file (error in input)
and suggested me to use feof() and ferror() for that.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)

Oct 15 '08 #11
arnuld said:
>On Tue, 14 Oct 2008 23:39:59 -0700, Keith Thompson wrote:
>Note that type char is an integer type. It's important to distinguish
between an integer type (of which there are several, including char,
int, unsigned long, etc.) and the specific integer type called "int".
The name "int" was obviously formed as an abbreviation of the word
"integer", but they mean different things.


Now I am much curious. Whats the different between an "integer" and a
variable of type "int". Do "integer types" are different from "int types"
char is an integer type, but it isn't an int. unsigned char and signed char
are other integer types that are not ints. Furthermore, we can qualify the
bare term "int" with, for example, "short" or "long", resulting in types
that are very similar in behaviour to bare-bones int, but with different
ranges: e.g. int's range is INT_MIN to INT_MAX, but short int's range is
SHRT_MIN to SHRT_MAX and long int's is LONG_MIN to LONG_MAX. And yet these
are integer types. Not ints, but integer types.

The difference isn't just one of ranges and types - it can also be one of
behaviour. The unsigned int type is an integer type that isn't an int. Its
range is 0 to UINT_MAX - but any results outside this range that you try
to store in it are reduced modulo (UINT_MAX + 1) so that they come within
range, and this behaviour is well-defined. This sure beats int's behaviour
when you try to store an out of range result.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '08 #12
arnuld wrote:
>On Tue, 14 Oct 2008 23:39:59 -0700, Keith Thompson wrote:
>Note that type char is an integer type. It's important to distinguish
between an integer type (of which there are several, including char,
int, unsigned long, etc.) and the specific integer type called "int".
The name "int" was obviously formed as an abbreviation of the word
"integer", but they mean different things.


Now I am much curious. Whats the different between an "integer" and a
variable of type "int". Do "integer types" are different from "int types"
The standard doesn't define any meaning for the phrase "int types". It
does define "integer types". "int" is the name one of one particular
integer type.

Integer types (6.2.5p17):
char

signed integer types (6.2.5p4):
standard signed integer types:
signed char, short int, int, long int, long long int

extended signed integer types (implementation-defined)

unsigned integer types (6.2.5p6):
standard unsigned integer types:
_Bool, and unsigned types corresponding to standard signed
integer types

extended unsigned integer types (implementation-defined)

enumerated types

It's not possible to be specific about the extended integer types. They
are implementation-defined types, such as _int36 for a 36-bit integer
type. In C90, such types were allowed only as an extension to C. This
meant that, in particular, things like size_t that were required to be
integer types could only be typedefs for standard types. In C99, the
concept of "extended integer types" was defined, and size_t is allowed
to refer any unsigned integer type, whether standard or extended.

....
Now I know why some clc lurker told me to distinguish between real end
of file (no more input) and the not so real end of file (error in input)
and suggested me to use feof() and ferror() for that.
EOF is just a macro name; it's clearly named in reference to "End Of
File", but it's also used by the character-oriented I/O functions as a
general-purpose error flag, not exclusively to refer to the end of the file.
Oct 15 '08 #13
arnuld wrote:
>On Tue, 14 Oct 2008 23:39:59 -0700, Keith Thompson wrote:
>Note that type char is an integer type. It's important to distinguish
between an integer type (of which there are several, including char,
int, unsigned long, etc.) and the specific integer type called "int".
The name "int" was obviously formed as an abbreviation of the word
"integer", but they mean different things.


Now I am much curious. Whats the different between an "integer" and a
variable of type "int". Do "integer types" are different from "int types"
The standard doesn't define any meaning for the phrase "int types". It
does define "integer types". "int" is the name one of one particular
integer type.

Integer types (6.2.5p17):
char

signed integer types (6.2.5p4):
standard signed integer types:
signed char, short int, int, long int, long long int

extended signed integer types (implementation-defined)

unsigned integer types (6.2.5p6):
standard unsigned integer types:
_Bool, and unsigned types corresponding to standard signed
integer types

extended unsigned integer types (implementation-defined)

enumerated types

It's not possible to be specific about the extended integer types. They
are implementation-defined types, such as _int36 for a 36-bit integer
type. In C90, such types were allowed only as an extension to C. This
meant that, in particular, things like size_t that were required to be
integer types could only be typedefs for standard types. In C99, the
concept of "extended integer types" was defined, and size_t is allowed
to refer any unsigned integer type, whether standard or extended.

....
Now I know why some clc lurker told me to distinguish between real end
of file (no more input) and the not so real end of file (error in input)
and suggested me to use feof() and ferror() for that.
EOF is just a macro name; it's clearly named in reference to "End Of
File", but it's also used by the character-oriented I/O functions as a
general-purpose error flag, not exclusively to refer to the end of the file.
Oct 15 '08 #14
arnuld wrote:
Mostly when I want to take input from stdin I use getchar() but I get this
from man page itself:

"If the integer value returned by getchar() is stored into a variable of
type char and then compared against the integer constant EOF, the
comparison may never succeed, because sign-extension of a variable of type
char on widening to integer is implementation-defined"
while( EOF != (ch = getchar()) ) ....
I use it like that. Can I run into problems with that ?

the function

int getchar();

reads a byte from the standard input and return it.
If End-of-file is read, it returns EOF (on my machine, it is 0xffffffff)
If ch is an int, there is no problem at all.
A common mistake is assigning getchar() into a char variable.
For example, if ch is a char:

EOF!=(ch=getcha r())

When the byte of 0xff is read:

getchar()=0x000 000ff
ch=0xff

Because EOF is an int, the value of ch is automatically casted to int.

If ch is unsigned, R.H.S of != is 0x000000ff
If ch is signed, R.H.S of != is 0xffffffff which is equal to EOF and
while loop will exit

Therefore, if ch is a char, there will be a problem if the read
character is expanded to EOF (which is implementation-specific) and the
signedness of char (again which is implementation-specific)
Oct 15 '08 #15
Michael wrote:
arnuld wrote:
....
> while( EOF != (ch = getchar()) ) ....
....
If ch is an int, there is no problem at all.
Unless INT_MAX<UCHAR_M AX, which is possible on systems where CHAR_BIT >=
16. On such systems, it's possible for a valid byte, when converted to
'int', to have the same value as EOF. The only work-around for that
possibility is to check feof() and ferror().
Oct 15 '08 #16
Michael <mi*****@michae ldadmum.no-ip.orgwrites:
[...]
the function

int getchar();

reads a byte from the standard input and return it.
If End-of-file is read, it returns EOF (on my machine, it is 0xffffffff)
[...]

No, EOF cannot be defined as 0xffffffff. It must expand to "an
integer constant expression, with type int and a negative value". A
typical definition is

#define EOF (-1)

If you convert the value of EOF to unsigned int on a 32-bit system,
the result is likely to be 0xffffffff; that's not the value of EOF,
it's the result of the conversion.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 15 '08 #17
Keith Thompson wrote:
Michael <mi*****@michae ldadmum.no-ip.orgwrites:
[...]
>the function

int getchar();

reads a byte from the standard input and return it.
If End-of-file is read, it returns EOF (on my machine, it is 0xffffffff)
[...]

No, EOF cannot be defined as 0xffffffff. It must expand to "an
integer constant expression, with type int and a negative value". A
typical definition is

#define EOF (-1)

If you convert the value of EOF to unsigned int on a 32-bit system,
the result is likely to be 0xffffffff; that's not the value of EOF,
it's the result of the conversion.
0xffffffff is hexadecimal *is* -1 in decimal on 32-bit int.
Oct 16 '08 #18
Michael wrote:
Keith Thompson wrote:
>If you convert the value of EOF to unsigned int on a 32-bit system,
the result is likely to be 0xffffffff; that's not the value of EOF,
it's the result of the conversion.

0xffffffff is hexadecimal *is* -1 in decimal on 32-bit int.
Not if it's an /unsigned/ int (see Keith's first sentence above).

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Oct 16 '08 #19
Michael wrote:
Keith Thompson wrote:
....
If you convert the value of EOF to unsigned int on a 32-bit system,
the result is likely to be 0xffffffff; that's not the value of EOF,
it's the result of the conversion.

0xffffffff is hexadecimal *is* -1 in decimal on 32-bit int.
Not in C. In C, 0xFFFFFFFF is just a different way of writing the same
value as 2147483647 - the only difference is that 0xFFFFFFFF might
have an unsigned type, while 2147483647 must have a signed type.
0xFFFFFFFF never has the meaning "-1". It can be converted to an int,
and if 'int' is a 32-bit 2's complement type the result of that
conversion will probably be -1, but that doesn't mean that 0xFFFFFFFF
is -1.
Oct 16 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
639
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets (data,5,stdin); the_number = atoi (data);
12
4099
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may have missed it). I was wondering wether it was just a 'common' behaviour of most systems, and wether the hit of the ENTER key was mandatory. Thanks for having read me.
1
3725
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if (!isspace(input)) j++;
5
8763
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
10
6616
by: john | last post by:
What does the standard say about getchar()? Do you have to press return to "send" the char to the program, or is it implementation defined? I read in a book that on some systems the function returns after you just typed the char, thus, it has no input buffer. Is that correct?
6
5288
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input characters - 'a' and '\n' (Hex 61 and 0a) It seems as though the getchar() function needs ENTER to terminate reading stdin. I am trying to get the program to respond when I press one key only (ie
11
2507
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1 #define OUT 0 int word_count();
25
5456
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
3
12508
by: mahiapkum | last post by:
Hello, I have a code which uses getchar(). #include<stdio.h> int main() { char ch; ch = getchar(); if(ch == 'Y') { printf("Beautiful World\n");
0
10509
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10219
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6954
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3977
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3081
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.