473,804 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basic c i/o and EOF

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

Nov 13 '05
28 4606
On Thu, 11 Sep 2003 21:00:07 UTC, dj******@csclub .uwaterloo.ca (Dave
Vandervies) wrote:
In article <wmzsGguTDN6N-pn2-1wd4V82b3Onx@mo on>,
The Real OS/2 Guy <os****@pc-rosenau.de> wrote:
On Thu, 11 Sep 2003 16:27:04 UTC, Johan Aurér <au***@axis.com > wrote:

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


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


I don't think that the OP uses a multibyte character sert. When he has
an single byte character set then the paragraph above is meaningless.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #21
On Fri, 12 Sep 2003 02:15:04 UTC, ne*****@tokyo.c om (Mantorok
Redgormor) wrote:
Irrwahn Grausewitz <ir*****@freene t.de> wrote in message news:<f3******* *************** **********@4ax. com>...
"The Real OS/2 Guy" <os****@pc-rosenau.de> wrote:
On Thu, 11 Sep 2003 16:27:04 UTC, Johan Aurér <au***@axis.com > wrote:

> On Thu, 11 Sep 2003, Irrwahn Grausewitz wrote:
>
> > Dave <no*****@noemai l.com> wrote:
> >
> > >
> > > long nc;
> > >
> > > nc = 0;
> > > while (getchar() != 'EOF') {
> > ^^^^^
> > 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. :-)
>
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:

When I use:

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

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


I'm happy to have a compiler that will give a diagnostic when the
locale is not set to a multibyte character set, but uses a single byte
one.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #22
On Thu, 11 Sep 2003 21:19:53 UTC, Richard Heathfield
<do******@addre ss.co.uk.invali d> wrote:
The Real OS/2 Guy wrote:
On Thu, 11 Sep 2003 16:27:04 UTC, Johan Aurer <au***@axis.com > wrote:
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.


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


Does you know the difference between multibyte and single byte
chararer sets?
A character is a
single char,


So 'a' is not a character, then?


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

It gets converted implicity to an int but it is none, else it were
impossible to assign it to an char without diagnostic.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #23
On Thu, 11 Sep 2003 22:03:53 UTC, Default User
<fi********@com pany.com> wrote:
The Real OS/2 Guy wrote:
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.

Why? Read it carefully! Get be informed on the difference between
multibyte and single byte character sets.
A multibyte character set uses multiple bytes to show a single char. A
singe byte character set only one.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #24
"The Real OS/2 Guy" <os****@pc-rosenau.de> wrote:

<SNIP>
I don't think that the OP uses a multibyte character sert. When he has
an single byte character set then the paragraph above is meaningless.


Which part of

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

do you consider meaningless???
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #25
"The Real OS/2 Guy" <os****@pc-rosenau.de> wrote:
On Thu, 11 Sep 2003 21:19:53 UTC, Richard Heathfield
<do******@addr ess.co.uk.inval id> wrote:
The Real OS/2 Guy wrote:
> On Thu, 11 Sep 2003 16:27:04 UTC, Johan Aurer <au***@axis.com > wrote:
>
>> 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.


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


Does you know the difference between multibyte and single byte
chararer sets?


Do you know?

<SNIP>

--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #26
"The Real OS/2 Guy" <os****@pc-rosenau.de> wrote:
On Thu, 11 Sep 2003 21:19:53 UTC, Richard Heathfield wrote:

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

It gets converted implicity to an int but it is none, else it were
impossible to assign it to an char without diagnostic.


This is simply wrong. Character constants in C are of type integer.
Lookup the standard on 'character constants'.
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #27
The Real OS/2 Guy wrote:
On Thu, 11 Sep 2003 21:19:53 UTC, Richard Heathfield
<do******@addre ss.co.uk.invali d> wrote:
The Real OS/2 Guy wrote:
> On Thu, 11 Sep 2003 16:27:04 UTC, Johan Aurer <au***@axis.com > wrote:
>
>> 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.
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'."


Does you know the difference between multibyte and single byte
chararer sets?


Yes.
> A character is a
> single char,


So 'a' is not a character, then?


It is.


Right, 'a' is a character, and 'a' is an int. Therefore, a character is not
a single char.
(Hint: 'a' is of type int, not char.)

It gets converted implicity to an int


No, it really really doesn't. It /is/ an int. AS USUAL, you sound very
confident but in fact you are utterly, utterly wrong. PLEASE understand
that you don't know as much about C as you thought you did. This is
something most of us go through when we start using this group; I know I
did, and lots of others here did too. But you seem to be slow to grasp
this. It's about time that you got a clue, and started to check your
opinions against an authoritative source before posting them.
but it is none, else it were
impossible to assign it to an char without diagnostic.


Nonsense.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #28
On Sat, 13 Sep 2003 05:39:23 +0000 (UTC), in comp.lang.c , "The Real
OS/2 Guy" <os****@pc-rosenau.de> wrote:
On Thu, 11 Sep 2003 21:19:53 UTC, Richard Heathfield
<do******@addr ess.co.uk.inval id> wrote:

So 'a' is not a character, then?
It is.

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

It gets converted implicity to an int but it is none,


Er, no, a single character is of type int. The standard says so in
6.4.4.4.
else it were
impossible to assign it to an char without diagnostic.


Balderdash.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
Nov 13 '05 #29

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

Similar topics

7
9293
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. # No warranty express or implied for the accuracy, fitness to purpose
9
2244
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script, FILE *in, FILE *out, FILE *err); It returns 0 on success or -1 on fail.
56
4143
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation error. (Not as infrequent as some bugs that have been discussed here in the past, but maybe once in an overnight run of the program when it was configured to aggressively exercise the section that the bug was in.) It was easy enough to trap the...
14
2515
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic types in objects. But if they are objects why it´s need this conversion? Aren´t objects (basic types) like Java?
3
2545
by: sefe dery | last post by:
hi ng, i try to create a asp.net 1.0 website on windows server 2003(Servername: ServerX) with iis 6.0. PROBLEM: The user should login with his windows credentials in basic.aspx and automatically redirect to his own files. i have the following file-structure:
13
3042
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format for accessing objects, controls, etc. in VB/Access2003. In particular where will I find explanations of:- Actions, Functions, Methods, Properties - I'm understand the
10
2670
by: trippeer | last post by:
I have the source code to an old BASIC program that a friend of mine would like to run online. I am a beginner at JS, but I think that it would be a good choice for the project. My background is in C/C++ and web development. Any suggestions that might get me off to a good start here? I can provide more information if needed, but I am not sure what would be helpful. The program is 550 lines in what appears to be BASIC and is a calendar...
97
5559
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in the first place. Anyone else heard about this development? The Master
111
5605
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks in advance! -entengk
6
3148
by: Simon Walsh | last post by:
I'm an Electronics student in college and I'm currently working on a project. I was given a circuit diagram for my project, from which I had to design a printed circuit board to be sent off and manufactured. I got my printed circuit board back and populated it with components. On my circuit board, I have a chip holder for a Basic STAMP microcontroller. To those unfamiliar with it, the Basic STAMP is a microcontroller which has an onboard...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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
10324
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,...
1
7623
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
6857
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();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
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.