473,666 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cannot understand the character handling Program

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
17 2127
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
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
"M.B" <mb*******@gmai l.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_Keit h) 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
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.c om, 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
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
"Chuck F. " <cb********@yah oo.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_Keit h) 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

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Chuck F. " <cb********@yah oo.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_Keit h) 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
"Keith Thompson" writes:
"Chuck F. " <cb********@yah oo.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
osmium wrote:
"Keith Thompson" writes:
"Chuck F. " <cb********@yah oo.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

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

Similar topics

7
1933
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas why the following code prints zeros for the message id, number of packets and sequence number as shown in the sample output below? Somehow I am not being able to access the contents of my 'incomingDataBuffer' array. I have also included some values from the respective MSVC 6.0 debug session. Thank you!
8
5464
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
7
7413
by: tuchka | last post by:
Hi, guys! I am very new here and just started to learn C. I have previous java exp. however. I'm abs. stuck on pointers and i'm unable comprehend algorithm of simple program that reverses chars in string no matter how long I'm staring at it. This is the method(I printed out some lines for better understanding(by me)):
26
21671
by: S!mb | last post by:
Hi all, I'm currently developping a tool to convert texts files between linux, windows and mac. the end of a line is coded by 2 characters in windows, and only one in unix & mac. So I have to delete a character at each end of a line. car = fgetc(myFile); while (car != EOF) {
1
2856
by: ssubbarayan | last post by:
Gurus, One of my friend mailed me this sample piece of code: #include <stdio.h> main() { int a,b,c; int count = 1; for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\ TFy!QJu ROo TNn(ROo)SLq SLq ULo+\ UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
0
1159
by: Lau Lei Cheong | last post by:
Hello, I'm writing a project that involves entering chinese character into MySQL database. Since 1) connection-based coding selection requires an upgrade to version 4.1.1 or above and my company don't want to do that, 2) the web program will run in both big5 and gb2312 so I can't simply set the coding to big5, and 3) my company's backend is written in VB6.0 which is they told me do not support unicode(so I can't change the database's...
3
1380
by: stormandstress | last post by:
Hi. I'm writing a program that is dependent on the curses library and functions for python, and I'm a little puzzled by the way characters are handled. The basics of the program are that a character is taken from input and put into a certain position within a list (There's more to it than that, but I think it's irrelevant). The problem is, when a character is taken via the <window>.getch() function, what comes back is an int...
10
1810
by: yaniv.dg | last post by:
hi all, i'm bumping into smething very starnge its very simple code but from some reason the program is acting strange this is my code: #include<stdio.h> #include<conio.h> void main(void) { int a,c; char b;
0
1708
by: norseman | last post by:
dudeja.rajat@gmail.com wrote: ============================== depending on OS and other factors, the DirList may be more like: ABDIR\ 41 42 44 49 52 5C 0A nextDir If so endswith needs to look for \\ and \n or just
0
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
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...
0
8640
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...
1
6198
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
5664
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.