Learning C

W

Wes

Hello,

I am trying to learn C from the book "C By Example". The first example
in the book is:

/* Filename: C2FIRST.C
Requests a name, prints the name 5 times, and rings a bell */

#include <stdio.h>
#define BELL '\a'

main()
{
int ctr=0; /* Integer variable to count through loop */
char fname[20]; /* Define character array to hold name */

printf("What is your first name?"); /* Prompt the user */
scanf(" %s", fname); /* Get the name from the
keyboard */
while (ctr < 5) /* Loop to print the name */
{ /* exactly 5 times */
printf("%s\n", fname);
ctr++;
}
printf("%c", BELL); /* Ring the terminals bell */
return;
}

The program compiles with no errors. During execution the name repeats
5 times but there is no BELL sound. Was this program possibly NOT
meant to run on a PC? The book is from 1994. I am using Windows ME
(yuck)

Thank you
 
U

Ulrich Eckhardt

Wes said:
#define BELL '\a'

That escape sequence is common, but handling it is up to the terminal, just
like colors, blinking, inverse etc. It might work on a different system.
main()
{ [...]
return;
}

The program compiles with no errors.

Hmmm, but hopefully not without warnings. In case you use the GCC, try
adding '-Wall' to the commandline. I believe it even should give you an
error, because you explicitly don't return anything ('return;') from a
function implicitly declared to return an int. I know that falling off the
end of main() is allowed, but an explicit return ...?

Uli
 
M

Mark McIntyre

#define BELL '\a'
The program compiles with no errors. During execution the name repeats
5 times but there is no BELL sound. Was this program possibly NOT
meant to run on a PC? The book is from 1994. I am using Windows ME

'\a' is the Alert escape sequence. How your OS interprets it is
platform-specific - you need to ask in a group specialising in your OS.
Maybe it flashes the window border, or briefly inverts the title bar or
something....

<OT>
On my PC (Win2K) it beeps the pc speaker ie the one inside the case.
Perhaps your PC doesn't have a case speaker. External speakers attached to
your sound card are not the same thing at all.
</OT>
 
J

Jarno A Wuolijoki

I believe it even should give you an
error, because you explicitly don't return anything ('return;') from a
function implicitly declared to return an int. I know that falling off the
end of main() is allowed, but an explicit return ...?

AFAIK it's a constraint violation in C99, but not in C89/90.
 
D

Dan Pop

In said:
AFAIK it's a constraint violation in C99, but not in C89/90.

It's explicitly allowed in C89:

If the main function executes a return that specifies no value,
the termination status returned to the host environment is undefined.

It's the falling off the end of main() that is allowed by implication
(because it's equivalent to executing a return statement that specifies
no value).

Dan
 
D

Dan Pop

In said:
Hello,

I am trying to learn C from the book "C By Example". The first example
in the book is:

/* Filename: C2FIRST.C
Requests a name, prints the name 5 times, and rings a bell */

#include <stdio.h>
#define BELL '\a'

main()
{
int ctr=0; /* Integer variable to count through loop */
char fname[20]; /* Define character array to hold name */

printf("What is your first name?"); /* Prompt the user */
scanf(" %s", fname); /* Get the name from the
keyboard */
while (ctr < 5) /* Loop to print the name */
{ /* exactly 5 times */
printf("%s\n", fname);
ctr++;
}
printf("%c", BELL); /* Ring the terminals bell */
return;
}

The program compiles with no errors. During execution the name repeats
5 times but there is no BELL sound. Was this program possibly NOT
meant to run on a PC? The book is from 1994. I am using Windows ME

Do yourself a favour and junk the book. Use K&R2 instead.

This program is not only far too complex to be used as the first example
in the book, it is also incorrect. One of the possible reasons you don't
hear anything is that the BELL character is not followed by a newline
character, as required by the C language.

But this is not the only problem of this program. What if the user's
first name is longer than 19 characters? The program doesn't make the
slightest attempt to protect itself against this possibility.

What if the user presses the end-of-file key instead of typing his first
name? Although trivial to check, the program ignores this possibility,
too.

Such a sloppy program doesn't belong to any C tutorial. The proper name
of your book is "C by Bad Example".

Dan
 
W

Wes

Thank you everyone and AHHHHHHHH!!! btw, there were no compiler
warnings. I am using Bloodshed Software Dev-C++ 4 compiler. Also, the
only reason I'm using this book is because I found it hanging around
in my house.
 
U

Ulrich Eckhardt

Wes said:
Thank you everyone and AHHHHHHHH!!! btw, there were no compiler
warnings. I am using Bloodshed Software Dev-C++ 4 compiler.

DevC++ comes with the GCC, you should be able to somewhere add '-Wall' to
the compiler arguments. I don't have it running here, or I would tell you
exactly where, sorry.

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top