basic c i/o and EOF

D

Dave

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 while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() {

long nc;

nc = 0;
while (getchar() != 'EOF') {
++nc;
}
printf("%ld\n", nc);

}

</code>

thanks
Dave
 
T

Tim Cambrant

Dave said:
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?

No, because when you type -1 it isn't stored as a character, but two
characters '-' and '1'. '-1' however is a single character, and those two
values doesn't match.

However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to '-' and '1'.
 
L

Lew Pitcher

No, because when you type -1 it isn't stored as a character, but two
characters '-' and '1'. '-1' however is a single character, and those two
values doesn't match.

However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to '-' and '1'.

Or better yet, if your platform supports it, enter the key or key combination
that signals end-of-input-data.

On MSDOS-based systems, this would be the ^Z key combination
On Unix-based systems, this would be what ever combination generates the 'eof'
signal (typically ^D, but check your stty settings to be sure).


--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 
I

Irrwahn Grausewitz

Dave said:
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?
Already addressed in the other replies.
It orks if i put something like 'q' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() {
int main( void ) {
long nc;

nc = 0;
while (getchar() != 'EOF') {
^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)
++nc;
}
printf("%ld\n", nc);
return 0;
}

</code>
AFAIK it is implementation defined how to produce an EOF via the
keyboard (e.g. Ctr-Z, Ctrl-C, Ctrl-D, ... ).
thanks
Dave

Regards

Irrwahn
 
D

Dan Pop

In said:
Already addressed in the other replies.

int main( void ) {

^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)

'EOF' is perfectly legal as a character constant, just not what the OP
needs in his program.

Dan
 
?

=?ISO-8859-1?Q?Johan_Aur=E9r?=

^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)

It's not "illegal," just implementation-defined. In fact, 'EOF' might
even equal EOF if you're lucky. :)
 
I

Irrwahn Grausewitz

Johan Aurér said:
It's not "illegal," just implementation-defined.
Hmph, you're so right.
In fact, 'EOF' might
even equal EOF if you're lucky. :)

I would call this bad luck, in that it would hide the fact that
one wrote unportable code. :)

Irrwahn
 
A

amanayin

Do you people know that code is taken from page 18
of The C programming language by kernigan & ritchie.
 
B

Ben Pfaff

Irrwahn Grausewitz said:
^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)

Not illegal, but its value is implementation-defined. It is
certainly not what the OP wants.
 
A

Andreas Kahari

Do you people know that code is taken from page 18
of The C programming language by kernigan & ritchie.

Yes. It is similar to, but not the same as the code on that
page in that book.
 
B

Ben Pfaff

amanayin said:
Do you people know that code is taken from page 18
of The C programming language by kernigan & ritchie.

I am sure that K&R would not use 'EOF' to test for end-of-file,
because they know how to write C.
 
J

John Bode

Dave said:
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?

When you type -1 at the command line, you're entering a string of
characters, not a single integral value. What getchar() sees in the
input stream is '-', '1', not -1.

Entering an EOF at the command line varies from system to system.
Some may not allow it at all. You're better off using a specific
character like 'q' for an explicit quit command.

You could try Ctrl-D or Ctrl-Z, but don't expect them to work
everywhere.
It orks if i put something like 'q' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() {

int main (void) {
long nc;

nc = 0;
while (getchar() != 'EOF') {

Lose the quotes around EOF.

while (getchar() != EOF) {
 
I

Irrwahn Grausewitz

amanayin said:
Do you people know that code is taken from page 18
of The C programming language by kernigan & ritchie.
Let's see...

OP> #include <stdio.h>
OP>
OP> void main() {
OP>
OP> long nc;
OP>
OP> nc = 0;
OP> while (getchar() != 'EOF') {
OP> ++nc;
OP> }
OP> printf("%ld\n", nc);
OP> }

K&R> #include <stdio.h>
K&R>
K&R> main()
K&R> {
K&R> long nc;
K&R>
K&R> nc = 0;
K&R> while (getchar() != EOF)
K&R> ++nc;
K&R> printf("%ld\n", nc);
K&R> }

Notice the two differences?
Hint: one invokes UB in OP's code.

Irrwahn
 
T

The Real OS/2 Guy

It's not "illegal," just implementation-defined. In fact, 'EOF' might
even equal EOF if you're lucky. :)
No, 'abc' or 'EOF' or 'BAD' is NOT a character. A character is a
single char, not a sequence of chars. A sequence of chars is known a
string, but a string must be in double quotes. So the compiler will
throw a diagnistic.
 
D

Dave Vandervies

ITYM "not lucky".

No, 'abc' or 'EOF' or 'BAD' is NOT a character. A character is a
single char, not a sequence of chars. A sequence of chars is known a
string, but a string must be in double quotes. So the compiler will
throw a diagnistic.

Quoth n869 (6.4.4.4):
--------
Description

[#2] An integer character constant is a sequence of one or
^^^^^^
more multibyte characters enclosed in single-quotes, as in
^^^^
'x' or 'ab'. A wide character constant is the same, except
prefixed by the letter L. With a few exceptions detailed
later, the elements of the sequence are any members of the
source character set; they are mapped in an implementation-
defined manner to members of the execution character set.
--------

So 'abc' is no less a character constant than 'a'. It may or may not
correspond to an acceptable char value, but it's perfectly acceptable as a
source code construct. (And, just for if you're going to try to nitpick
on the 'may not correspond to an acceptable value', I'll point out that
'a' isn't a "character" either, only a character constant whose value
(of integer type) corresponds to the appropriate character.)


dave
 
I

Irrwahn Grausewitz

The Real OS/2 Guy said:
No, 'abc' or 'EOF' or 'BAD' is NOT a character.
It's not a character, it's a (integer) character constant with
implementation-defined value.

From WG14/N843, section 6.4.4.4:

[...]

[#2] An integer character constant is a sequence of one or
more multibyte characters enclosed in single-quotes, as in
'x' or 'ab'. A wide character constant is the same, except
prefixed by the letter L. With a few exceptions detailed
later, the elements of the sequence are any members of the
source character set; they are mapped in an implementation-
defined manner to members of the execution character set.

[...]

[#10] An integer character constant has type int. The value
of an integer character constant containing a single
character that maps to a member of the basic execution
character set is the numerical value of the representation
of the mapped character interpreted as an integer. The
value of an integer character constant containing more than
one character, or containing a character or escape sequence
not represented in the basic execution character set, is
implementation-defined. If an integer character constant
contains a single character or escape sequence, its value is
the one that results when an object with type char whose
value is that of the single character or escape sequence is
converted to type int.

<SNIP>

Irrwahn
 
R

Richard Heathfield

The said:
No, 'abc' or 'EOF' or 'BAD' is NOT a character.

It is, however, a character constant.

The Standard says: "An integer character constant is a sequence of one or
more multibyte characters enclosed in single-quotes, as in 'x' or 'ab'."
A character is a
single char,

So 'a' is not a character, then?

(Hint: 'a' is of type int, not char.)
 
D

Default User

The said:
No, 'abc' or 'EOF' or 'BAD' is NOT a character. A character is a
single char, not a sequence of chars. A sequence of chars is known a
string, but a string must be in double quotes. So the compiler will
throw a diagnistic.


That must come as a shock to the guys who wrote the Standard:


[#2] An integer character constant is a sequence of one or
more multibyte characters enclosed in single-quotes, as in
'x' or 'ab'. A wide character constant is the same, except
prefixed by the letter L. With a few exceptions detailed
later, the elements of the sequence are any members of the
source character set; they are mapped in an implementation-
defined manner to members of the execution character set.




Brian Rodenborn
 
M

Mantorok Redgormor

Irrwahn Grausewitz said:
The Real OS/2 Guy said:
No, 'abc' or 'EOF' or 'BAD' is NOT a character.
It's not a character, it's a (integer) character constant with
implementation-defined value.

From WG14/N843, section 6.4.4.4:

[...]

[#2] An integer character constant is a sequence of one or
more multibyte characters enclosed in single-quotes, as in
'x' or 'ab'. A wide character constant is the same, except
prefixed by the letter L. With a few exceptions detailed
later, the elements of the sequence are any members of the
source character set; they are mapped in an implementation-
defined manner to members of the execution character set.

[...]

[#10] An integer character constant has type int. The value
of an integer character constant containing a single
character that maps to a member of the basic execution
character set is the numerical value of the representation
of the mapped character interpreted as an integer. The
value of an integer character constant containing more than
one character, or containing a character or escape sequence
not represented in the basic execution character set, is
implementation-defined. If an integer character constant
contains a single character or escape sequence, its value is
the one that results when an object with type char whose
value is that of the single character or escape sequence is
converted to type int.

<SNIP>

Irrwahn

When I use:

printf("%c\n", 'EOF');

It prints 'F', is it suppose to print that or am I invoking undefined behavior?
 
I

Irrwahn Grausewitz

Irrwahn Grausewitz said:
"The Real OS/2 Guy" <[email protected]> wrote:
No, 'abc' or 'EOF' or 'BAD' is NOT a character.
It's not a character, it's a (integer) character constant with
implementation-defined value.

From WG14/N843, section 6.4.4.4:

[...]

[#2] An integer character constant is a sequence of one or
more multibyte characters enclosed in single-quotes, as in
'x' or 'ab'. A wide character constant is the same, except
prefixed by the letter L. With a few exceptions detailed
later, the elements of the sequence are any members of the
source character set; they are mapped in an implementation-
defined manner to members of the execution character set.

[...]

[#10] An integer character constant has type int. The value
of an integer character constant containing a single
character that maps to a member of the basic execution
character set is the numerical value of the representation
of the mapped character interpreted as an integer. The <
value of an integer character constant containing more than <
one character, or containing a character or escape sequence <
not represented in the basic execution character set, is <
implementation-defined. If an integer character constant <
contains a single character or escape sequence, its value is
the one that results when an object with type char whose
value is that of the single character or escape sequence is
converted to type int.

<SNIP>

When I use:

printf("%c\n", 'EOF');

It prints 'F', is it suppose to print that or am I invoking undefined behavior?

It prints whatever the implementor intended. I've marked the relevant
section in the quote above with '<'s. You are not invoking any UB, but
the result is implementation-defined. Thus, you cannot use it in
portable code, but as long as you stick to one implementation you may
use it, for whatever purpose. If you want to find out what the value
of such a multi-character-constant is like, you have to consult the
documentation that comes with your compiler.

Regards

Irrwahn
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top