By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,125 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,125 IT Pros & Developers. It's quick & easy.

cannot understand the character handling Program

P: n/a
When I am trying to execute a program from "The C Programming Language"
by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.
#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am not
able to reach the EOF as specified in the While statement even after
giving a considerable inputs. I have 2 questions.

1st is I simply cannot understand the logic of the program
even after going through it.
Whatever the input we are typing in is appearing in the window. Is
there any where the data is being stored. I would be thankful if
someone could explain the use of the program in layman's language.

2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?. One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.

Please help me to understand it.

Jan 4 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
M.B

Gladiator wrote:
When I am trying to execute a program from "The C Programming Language"
by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.
#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am not
able to reach the EOF as specified in the While statement even after
giving a considerable inputs. I have 2 questions.
first of all understand the program.
this takes input from standard input (keyboard by default - getchar
does it) . count the number of characters typed and displays the count
on end of input (in standard output - terminal). the end of input is
EOF (end of file). I am not sure of DOS but in unix control+d gives EOF

1st is I simply cannot understand the logic of the program
even after going through it.
Whatever the input we are typing in is appearing in the window. Is
there any where the data is being stored. I would be thankful if
someone could explain the use of the program in layman's language.

2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?. One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.
EOF is not any maximum defined by size of anything. it indicated that
thatere are no more input available. it is defined in stdio . h and in
unix it is ctrl+d.
EOF is present at the end of all files (atleast in unix)

if ctrl+d dont work in ur OS for EOF pls refer to OS manual for it
Please help me to understand it.


Jan 4 '06 #2

P: n/a
Gladiator wrote:
#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am not
able to reach the EOF as specified in the While statement even after
giving a considerable inputs.
The program loops as long as another character can be obtained from the
input stream stdin. If you are using keyboard input (in the absence of
piping from a file or other program), it continues to accept characters
until you somehow terminate the input stream. The termination is OS
specific, usually a special key or key sequence.
1st is I simply cannot understand the logic of the program
even after going through it.
Whatever the input we are typing in is appearing in the window. Is
there any where the data is being stored.
Not by your program.
I would be thankful if
someone could explain the use of the program in layman's language.
getchar() reads the next input character from the standard input stream.
If there are no more characters, EOF (for End of File) is returned
instead of a character. Each time the return value is not EOF, nc is
incremented.

When there no more characters, the loop terminates and the printf
statement should print the number of characters read. Your program uses
an incorrect format, %1d, which should be %ld, because the variable nc
is a long int.
2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?.
Yes. Fortunately, that is already done is <stdio.h>, which you included
prior to using EOF.
One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.


EOF is an integer value that is separate from all input character values
converted to an unsigned int (we are handling simple type char
characters here, not wide characters). Typically it is -1, but it may
vary from one implementation to another.

--
Thad
Jan 4 '06 #3

P: n/a
"M.B" <mb*******@gmail.com> writes:
Gladiator wrote:

[snip]
2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?. One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.

EOF is not any maximum defined by size of anything. it indicated that
thatere are no more input available. it is defined in stdio . h and in
unix it is ctrl+d.
EOF is present at the end of all files (atleast in unix)

if ctrl+d dont work in ur OS for EOF pls refer to OS manual for it


Both of you have misunderstood what EOF is all about.

Each file (including stdin) has an end-of-file indicator. This isn't
a character or a value, it's a condition; it indicates that the last
attempt to read from the file failed because it had reached the end of
the file.

EOF is a macro defined in <stdio.h>. It specifies the value that will
be returned by certain input functions, such as getchar(), on an
end-of-file or error condition. The value of EOF isn't specified by
the standard except that it's a negative integer (it's typically -1).
The getchar() function attempts to read a character. If it was
successful, it returns the value of that character, as an unsigned
char converted to int. (On a typical system, this will be a value in
the range 0..255.) If it failed, it returns the value EOF, which is
distinct from any valid character value.

A system will typically have some mechanism for the user to cause an
end-of-file condition when a program is reading from an interactive
device such as a keyboard. Under Unix, this is typically indicated by
typing control-D. Under MS-DOS, it's usually control-Z. Your program
won't see the control-D (4) or control-Z (26) value; it will instead
see the value EOF.

A disk file may or may not have some marker at the end of it to
indicate the end of the file. Under MS-DOS, a text file might have a
control-Z character at the end; under Unix, the system simply keeps
track of how big the file is.

--
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.
Jan 4 '06 #4

P: n/a
Gladiator wrote:

When I am trying to execute a program from "The C Programming
Language" by Dennis Ritchie, I tried to run the following
program. I am using Dev++ as a compiler software. The Program
is presented below. >

#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am
not able to reach the EOF as specified in the While statement
even after giving a considerable inputs. I have 2 questions.

1st is I simply cannot understand the logic of the program even
after going through it. Whatever the input we are typing in is
appearing in the window. Is there any where the data is being
stored. I would be thankful if someone could explain the use of
the program in layman's language.
Understanding the program is greatly enhanced by proper
indentation. Compare the version below:

#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

and I have omitted complaining about the lack of "return 0" or
accurate declaration of main.

2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it
in a comparison?. One way what i could think is the EOF value
may be the maximum value that the variable can hold ( In this
"Long"). But I am not sure about it.


EOF is a macro, which you loaded when you #included <stdio.h>. You
don't care what the value is, just that it is unique, and different
from any possible char value.

Notice that the return from getchar() is only tested, never stored
(in this program). It is of type int, which allows it to take on
values outside the range of char, such as EOF.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 4 '06 #5

P: n/a
wow!
I must say that this is an eye opener for me. All this time i was under
the impression that EOF is a special character. Coming from a Cobol
background sentences like OPEN <FILE> AT END.. verbs, and because of
MPE/iX intrinsics like FWRITEDIR which 'writes' EOF marker at the end
of last record in file, I was 'convinced' that EOF is a character like
CR/LF which is present in the file. I somehow never thought that it
could be a condition not a character.

Thanx for the nice explanation.
~yogesh

Jan 4 '06 #6

P: n/a
"Chuck F. " <cb********@yahoo.com> writes:
[...]
EOF is a macro, which you loaded when you #included <stdio.h>. You
don't care what the value is, just that it is unique, and different
from any possible char value.

Notice that the return from getchar() is only tested, never stored
(in this program). It is of type int, which allows it to take on
values outside the range of char, such as EOF.


A quibble: EOF commonly is in the range of char on systems where plain
char is signed. getchar() returns the next character as an unsigned
char converted to an int; EOF, since it's negative, is guaranteed to
be outside the range of unsigned char.

--
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.
Jan 4 '06 #7

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Chuck F. " <cb********@yahoo.com> writes:
[...]
EOF is a macro, which you loaded when you #included <stdio.h>. You
don't care what the value is, just that it is unique, and different
from any possible char value.

Notice that the return from getchar() is only tested, never stored
(in this program). It is of type int, which allows it to take on
values outside the range of char, such as EOF.


A quibble: EOF commonly is in the range of char on systems where plain
char is signed. getchar() returns the next character as an unsigned
char converted to an int; EOF, since it's negative, is guaranteed to
be outside the range of unsigned char.

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

Jan 4 '06 #8

P: n/a
"Keith Thompson" writes:
"Chuck F. " <cb********@yahoo.com> writes:
[...]
EOF is a macro, which you loaded when you #included <stdio.h>. You
don't care what the value is, just that it is unique, and different
from any possible char value.

Notice that the return from getchar() is only tested, never stored
(in this program). It is of type int, which allows it to take on
values outside the range of char, such as EOF.


A quibble: EOF commonly is in the range of char on systems where plain
char is signed. getchar() returns the next character as an unsigned
char converted to an int; EOF, since it's negative, is guaranteed to
be outside the range of unsigned char.


And what exactly, is the quibble? He said it was an int. Are you saying he
was wrong? Or what? Do you mean to suggest that it can, in some
circumstances, be detected as a char? I don't think that what was going
through the mind of a compiler writer as he wrote some code is terribly
interesting.
Jan 4 '06 #9

P: n/a
osmium wrote:
"Keith Thompson" writes:
"Chuck F. " <cb********@yahoo.com> writes:
[...]
EOF is a macro, which you loaded when you #included <stdio.h>. You
don't care what the value is, just that it is unique, and different
from any possible char value.

Notice that the return from getchar() is only tested, never stored
(in this program). It is of type int, which allows it to take on
values outside the range of char, such as EOF.


A quibble: EOF commonly is in the range of char on systems where plain
char is signed. getchar() returns the next character as an unsigned
char converted to an int; EOF, since it's negative, is guaranteed to
be outside the range of unsigned char.


And what exactly, is the quibble? He said it was an int. Are you saying he
was wrong?


CBF said "values outside the range of char, such as EOF".
Keith's quibble was that EOF is actually inside the range of char
on many (most?) systems, including the OP's system.

Jan 4 '06 #10

P: n/a
On 4 Jan 2006 09:52:34 -0800, "yogeshmk" <yo***************@gmail.com>
wrote in comp.lang.c:
wow!
I must say that this is an eye opener for me. All this time i was under
the impression that EOF is a special character. Coming from a Cobol
background sentences like OPEN <FILE> AT END.. verbs, and because of
MPE/iX intrinsics like FWRITEDIR which 'writes' EOF marker at the end
of last record in file, I was 'convinced' that EOF is a character like
CR/LF which is present in the file. I somehow never thought that it
could be a condition not a character.

Thanx for the nice explanation.
~yogesh


On some platforms, under some circumstances, it is. At least for text
files.

Early OS's for small platforms (specifically CP/M 80), did not track
the actual size of a file in octets, only in logical sectors, which
happened to 128 octets. When you read the last sector of a file into
memory, you had no real way of telling how many of the 128 octets were
part of the data, and how many were just left-over junk.

For binary files, you were on your own, but for text files the
convention was that the program that wrote the file appended a 0x1a
after the last actual valid byte.

Early versions of MS-DOS used the same convention.

I am resurrecting some code I wrote in the mid 1980's on MS-DOS. When
I open them in a modern GUI editor, there's a little square box at the
end of each, what Windows produces for characters that don't have a
glyph in the font in use.

But even in the days of CP/M 80 and MS-DOS 2.x, if a file was opened
in text mode, the C stdio library never let the program see the 0x1a,
it returned EOF when that octet was read.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 5 '06 #11

P: n/a
1. The logic of the program
getchar () have been used here . whenever a cprogram starts three
shandard files are opened named stdin , stdout , and stderr . if u dont
specify any file as input to the program :
c:\program.exe <file1.txt
then it will asuume stdin standard stream attached to the keyboard as
its input and thus getchar gets it's input from stdin i.e. keyborad and
simply outputs it till u enter end of file.
u may rum the program in tow ways.
c:\program.exe
in this case stdin is taken as input stream attached to the program and
program just prints whatever u type on the screen
2.as u have asked about EOF
EOF is defined as
#defined EOF -1
in file stdio.h
it may have different value on your systems. this constants and severl
opthers like it make c programs portable to across different plateforms
value of EOF may be diffeerent for C compilers designed forb different
plateforms like windows and unix.
Gladiator
Jan 4, 10:28 am show options

Newsgroups: comp.lang.c
From: "Gladiator" <siva...@gmail.com> - Find messages by this author
Date: 3 Jan 2006 21:28:31 -0800
Local: Wed, Jan 4 2006 10:28 am
Subject: cannot understand the character handling Program
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

When I am trying to execute a program from "The C Programming Language"

by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc); // I think it should be %ld in
//in place of %1d
}
and there is no need for long , simple int will work since
EOF value may be out of range of charectyers and also getchar() outputs
an integer type u may have used int in place of long nc;
hope this may be of some help to you.
Go on Programming.
Bye

Jan 5 '06 #12

P: n/a
1. The logic of the program
getchar () have been used here . whenever a cprogram starts three
shandard files are opened named stdin , stdout , and stderr . if u dont
specify any file as input to the program :
c:\program.exe <file1.txt
then it will asuume stdin standard stream attached to the keyboard as
its input and thus getchar gets it's input from stdin i.e. keyborad and
simply outputs it till u enter end of file.
u may rum the program in tow ways.
c:\program.exe
in this case stdin is taken as input stream attached to the program and
program just prints whatever u type on the screen
2.as u have asked about EOF
EOF is defined as
#defined EOF -1
in file stdio.h
it may have different value on your systems. this constants and severl
opthers like it make c programs portable to across different plateforms
value of EOF may be diffeerent for C compilers designed forb different
plateforms like windows and unix.
Gladiator
Jan 4, 10:28 am show options

Newsgroups: comp.lang.c
From: "Gladiator" <siva...@gmail.com> - Find messages by this author
Date: 3 Jan 2006 21:28:31 -0800
Local: Wed, Jan 4 2006 10:28 am
Subject: cannot understand the character handling Program
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

When I am trying to execute a program from "The C Programming Language"

by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc); // I think it should be %ld in
//in place of %1d
}
and there is no need for long , simple int will work since
EOF value may be out of range of charectyers and also getchar() outputs
an integer type u may have used int in place of long nc;
hope this may be of some help to you.
Go on Programming.
Bye

Jan 5 '06 #13

P: n/a
vishnu said:
1. The logic of the program
getchar () have been used here . whenever a cprogram starts three
shandard files are opened named stdin , stdout , and stderr . if u dont
specify any file as input to the program :
c:\program.exe <file1.txt
The C Standard does not guarantee that this is the right way to specify
input from a file. Indeed, your command line shows severe signs of
non-portability. For example, it certainly doesn't work on my machine.
then it will asuume stdin standard stream attached to the keyboard as
its input and thus getchar gets it's input from stdin i.e. keyborad and
simply outputs it till u enter end of file.
The C Standard does not guarantee that a keyboard is attached to the system.
Nor does it dictate which device is attached to stdin.
u may rum the program in tow ways.
c:\program.exe
This doesn't work on my machine. What makes you think it'll work on the OP's
machine?
in this case stdin is taken as input stream attached to the program and
program just prints whatever u type on the screen
2.as u have asked about EOF
EOF is defined as
#defined EOF -1
No, it's not. Firstly, the above is a syntax error. Secondly, the true
definition of EOF in language terms is: "a negative integral constant
expression that is returned by several functions to indicate end-of-file
,that is, no more input from a stream;"

So it might, or might not, be -1.
it may have different value on your systems.
Right. So there wasn't much point in giving a value of -1, was there? Still,
full marks for pointing this out. We can, however, state categorically that
its value is /negative/.
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc); // I think it should be %ld in
//in place of %1d
Please don't use // when you mean /* */

Yes, you are correct; it should be %ld rather than %1d ("ell" rather than
"one") - better still would be to change nc to unsigned long and use %lu.
and there is no need for long , simple int will work since
You cannot guarantee that.
EOF value may be out of range of charectyers and also getchar() outputs
an integer type u may have used int in place of long nc;


You have misunderstood the reason long int was used. It is not storing the
result of getchar(). It is being used as a counter.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 5 '06 #14

P: n/a
"Old Wolf" writes:
osmium wrote:
"Keith Thompson" writes:
"Chuck F. " <cb********@yahoo.com> writes:
[...]
EOF is a macro, which you loaded when you #included <stdio.h>. You
don't care what the value is, just that it is unique, and different
from any possible char value.

Notice that the return from getchar() is only tested, never stored
(in this program). It is of type int, which allows it to take on
values outside the range of char, such as EOF.

A quibble: EOF commonly is in the range of char on systems where plain
char is signed. getchar() returns the next character as an unsigned
char converted to an int; EOF, since it's negative, is guaranteed to
be outside the range of unsigned char.


And what exactly, is the quibble? He said it was an int. Are you saying
he
was wrong?


CBF said "values outside the range of char, such as EOF".
Keith's quibble was that EOF is actually inside the range of char
on many (most?) systems, including the OP's system.


He said *allows* it to take on. Allowing something and doing something are
two different things. But I think you found the point he was trying to
make, thanks.
Jan 5 '06 #15

P: n/a
hye
i have read your mail the coding which you have given insted of
this if you use following
code it will run properly.
#include <stdio.h>
main()
{
char ch;
long nc = 0;
while((ch=getchar())!= EOF)
++nc;
printf("%ld\n",nc);
}

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

now i will try to explain EOF first
EOF is a special macro define in stdio.h it is used to represent the
end of file.
its value is ^z which can be obtain on output screen by pressing (ctrl
Z).
EOF is the special character used in file handelling.Using file
handelling we can read any file
saved on our system.Now while reading the file how we will come to
know that we have reached to the end of file? for that purpose EOF is
used
I think this not much detail
i will reply again.

Jan 5 '06 #16

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pank wrote:
hye
i have read your mail the coding which you have given insted of
this if you use following
code it will run properly.
#include <stdio.h>
main()
{
char ch;
long nc = 0;
while((ch=getchar())!= EOF)
++nc;
printf("%ld\n",nc);
}

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

now i will try to explain EOF first
EOF is a special macro define in stdio.h it is used to represent the
end of file.
its value is ^z which can be obtain on output screen by pressing (ctrl
Z).


Bzzzt. Wrong.

EOF is a condition, not a value.

The /condition/ is that there is no more data to be retrieved from the input stream.

The /condition/ can be initiated by various means, all of which are platform
dependant.

On MSDOSish systems, the input of a <ctrl>Z often tells the system to signal the
/condition/ of no more data (EOF) to programs reading the file or interactive
stream on which the <ctrl>Z is entered. This is not the only way the condition
is initiated; when reading a binary stream from file, MSDOSish systems signal
the End_Of_File condition when there is no more physical data to be read (the
read position exceeds the number of bytes in the file).

On Unixish systems, the input of the 'eof' character (user defined) on an
interactive device causes the OS to signal to readers that there is no more
data. OTOH, there is no corresponding characters for file streams (text or
binary); end-of-file is established by the exhaustion of data on the media, and
the OS signals End_Of_File to the readers.

In all cases, the C runtime is responsible for interpreting the reported
End_Of_File condition and presenting the EOF to the program code.

So, lets recap.

EOF represents a condition which is detected by the OS (by whatever OS dependant
means) and reported to the program by way of the C runtime.

There are various means to invoke the condition, but they are platform
dependant. Some means include entering <ctrl>Z <enter> as the first characters
of a new line (MSDOSish systems) or entering <ctrl>Z as a byte in a text stream
(MSDOSish systems), or by entering a user definable character as a character in
an interactive stream (Unixish systems), or by physically exhausting the data
source (MSDOSish systems, Unixish systems).

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDvU4xagVFX4UWr64RAhb8AJ9PnRP1QvIeJLZbTGxGiE Zx1xlzCwCg4C5X
DWedZSJ7F01Lm/TaX5A0dJc=
=GeFQ
-----END PGP SIGNATURE-----
Jan 5 '06 #17

P: n/a
Lew Pitcher <Le*********@td.com> writes:
pank wrote:

[...]
EOF is a special macro define in stdio.h it is used to represent the
end of file.
its value is ^z which can be obtain on output screen by pressing (ctrl
Z).


Bzzzt. Wrong.

EOF is a condition, not a value.


Um, not quite. End-of-file is a condition. EOF is a macro which
expands to an expression whose value is used to indicate the
end-of-file condition (it can also indicate an error condition).

--
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.
Jan 5 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.