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
Dave <no*****@noemai l.com> wrote in message news:<bj******* ***@titan.btint ernet.com>... 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) { ++nc; } printf("%ld\n", nc);
}
</code>
thanks Dave
amanayin <ng******@netsc ape.net> writes: 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.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x1f6}, *p=
b,x,i=24;for(;p +=!*p;*p/=4)switch(x=*p& 3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
amanayin <ng******@netsc ape.net> wrote: 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
--
Close your eyes and press escape three times.
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. 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.
--
Tschau/Bye
Herbert
eComStation 1.1 Deutsch Beta ist verügbar
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.)
dave
--
Dave Vandervies dj******@csclub .uwaterloo.ca
Fortunately programmers who actually use those anomalous cases are few,
and getting fewer every time we catch up to one.
--David Thompson in comp.lang.c
"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:
[...]
[#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
--
I can't see it from here, but it looks good to me.
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'."
A character is a single char,
So 'a' is not a character, then?
(Hint: 'a' is of type int, not char.)
--
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
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.
Brian Rodenborn
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:
[...]
[#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? 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:
<SNIP>No, 'abc' or 'EOF' or 'BAD' is NOT a character. It's not a character, it's a (integer) character constant with implementatio n-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
--
I can't see it from here, but it looks good to me. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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.
|
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...
|
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?
|
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:
| |
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
|
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...
|
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
|
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
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |