473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Silly Question

1
Hello,

I'm new to this form and new to C programming. I'm starting to work my way through Kernighan and Ritchie's book, but I am having trouble getting some of their examples to work. For instance when I compile and run:

#include <stdio.h>

main()
{
long nc;

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

This program is suppose to count the number of characters, but when I run it and type: the+enter, nothing happens. Even if I let c = getchar() etc., still nothing happens. I'm using the lcc-win32 IDE.

Thanks,
Mike
Jul 19 '07 #1
4 1445
sicarie
4,677 Expert Mod 4TB
mlukas-

Welcome to theScripts!

I'm betting your issue is coming from your while condition.

EOF in C/C++ is actually a character. You can find out what it is on your compiler by printing it, but my point is that you cannot input an EOF character with the keyboard. Try changing it to a letter, ie

while (getchar() != 'a')

and then running it (not pressing a - or whatever character you used - until you want to exit).
Jul 19 '07 #2
Banfa
9,065 Expert Mod 8TB
I'm betting your issue is coming from your while condition.

EOF in C/C++ is actually a character. You can find out what it is on your compiler by printing it, but my point is that you cannot input an EOF character with the keyboard.
CTRL-Z (ASCII 26) is the normal end of line character and you can input it from the keyboard (by press control and Z and the same time :D ) if you run the program and give this input

the<Enter>
<CTRL-Z><enter>

Then the program correctly outputs the the value 4, the number of characters that appear before the CTRL-Z.

However that is a ratrher convoluted way to get keyboard input and using a normal character (I would have chosen newline '\n') is probably a better option.
Jul 20 '07 #3
sicarie
4,677 Expert Mod 4TB
CTRL-Z (ASCII 26) is the normal end of line character and you can input it from the keyboard (by press control and Z and the same time :D ) if you run the program and give this input

the<Enter>
<CTRL-Z><enter>

Then the program correctly outputs the the value 4, the number of characters that appear before the CTRL-Z.

However that is a ratrher convoluted way to get keyboard input and using a normal character (I would have chosen newline '\n') is probably a better option.
I did not know that. Thanks Banfa.
Jul 20 '07 #4
JosAH
11,448 Expert 8TB
Hello,

I'm new to this form and new to C programming. I'm starting to work my way through Kernighan and Ritchie's book, but I am having trouble getting some of their examples to work. For instance when I compile and run:

#include <stdio.h>

main()
{
long nc;

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

This program is suppose to count the number of characters, but when I run it and type: the+enter, nothing happens. Even if I let c = getchar() etc., still nothing happens. I'm using the lcc-win32 IDE.

Thanks,
Mike
Does the front cover of your Kernighan & Ritchie copy mention "Ansi" or "2nd
edition" somewhere? If not, you have a very old copy of their book.

kind regards,

Jos
Jul 20 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
4
by: musosdev | last post by:
I think I'm having a dim day and should just go back to bed, but alas, I cant do that.... I'm writing a peice of code to create XHTML compliant documents using System.IO, there's probably an...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
2
by: toton | last post by:
Hi, This is a silly question related to syntax only. I have a template class with template member function, How to write it separately in a file (not translation unit, just want to separate...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.