prob w/the C prog. lang. book(ANSI C)

M

Matt Suther

I just bought The C Programming Language, by the guy who made C and
so...I have 2 different compilers, free ones, and I don't know if I
typed the code in wrong or if it's a problem with my compilers, do any
of you know? They were free compilers: Icc-win32 and Bloodshed Dev
C++(uses C too). Are there and compilers that are free besides mars,
and gcc-whatever, can't get them to work. Thanks.

heres the code, is there anything wrong with it:

#include <stdio.h>

main()
{
int fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
 
M

Malcolm

Matt Suther said:
Are there and compilers that are free besides mars,
and gcc-whatever, can't get them to work. Thanks.

heres the code, is there anything wrong with it:
You don't say what is going wrong. The code you posted should produce a
program with output.
Remember that there are three stages to testing a C program. First you have
to compile it. The compiler will complain about syntax errors, headers it
can't find, and similar. Once you compile you have to link with libraries.
This step will usually but not always be invoked automatically by the
compiler. Finally you have to run the executable file you have created. If
you are using a free compiler, usually this will have to be done by setting
a shell/command prompt to the directory in which you created the executable,
and running it by typing the name.
At which stage do things go wrong?
 
M

Martin Ambuhl

Matt said:
I just bought The C Programming Language, by the guy who made C and
so...I have 2 different compilers, free ones, and I don't know if I
typed the code in wrong or if it's a problem with my compilers,

The problem is your typing.
[...]
heres the code, is there anything wrong with it:

#include <stdio.h>

main()

main returns an int. You should say so (and must for C99):
int main(void)
{
int fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
^^^^^^^^^^^^^^^^^
Your typing error
The program is a bit worse than that, since even after you correct your
typing error, a computed value of, say, 211.99999 will be stored in
celsius as 211.
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}

main returns an int, and you should say so (although C99 added that
without the return, a program will now act as though the line below were
there):
return 0;
 
J

Jens.Toerring

Martin Ambuhl said:
^^^^^^^^^^^^^^^^^
Your typing error

Sorry, but what typo? In my copy of K&R2 (from 1988) it looks exactly
like the OP copied it (and neither seems that page/line to be mentioned
in the errata list nor do I see anything obviously wrong with it).

Regards, Jens
 
J

JaSeong Ju

I copied and pasted the posted code and compiled
fine. No errors or warnings. I used gcc 3.2.2 compiler
on RH9.

Here is the output:
bash-2.05b$ ./a.out
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
bash-2.05b$

There should be int in front of main and return 0
before the last }. Also since you're computing the
values, the variables should be either float or double.
But aside from this, the code seems fine.
 
J

JaSeong Ju

I copied and pasted the posted code and compiled
fine. No errors but there were 2 warnings. I used
gcc 3.2.2 compiler on RH9.

Here is the output:
bash-2.05b$ ./a.out
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
bash-2.05b$

There should be int in front of main and return 0
before the last }. These were the 2 warnings about.

Also since you're computing the
values, the variables should be either float or double.
But aside from this, the code seems fine.
 
M

Martin Ambuhl

Sorry, but what typo? In my copy of K&R2 (from 1988) it looks exactly
like the OP copied it (and neither seems that page/line to be mentioned
in the errata list nor do I see anything obviously wrong with it).

Please cite the page on which you claim to have found this program as
Sather posted it.
If that is not a typing error, then either
int fahr, celsius; and
> printf("%d\t%d\n", fahr, celsius);
are typing errors, or the program is presented as an example of a broken
program.

As it happens, the program on page of K&R1 (1978) has none of those
three lines as shown in the posting by Mark Sather. On the left is the
code Sather posted; indented is the code from K&R1 when it differs:

/* Sather's posting */
> #include <stdio.h>
> main()
> {
> int fahr, celsius;
/* K&R has this declarion below that for lower, upper, step,
and has it as: */
float fahr, celsius;
> int lower, upper, step;
> lower = 0;
> upper = 300;
> step = 20;
> fahr = lower;
> while (fahr <= upper) {
> celsius = 5 * (fahr-32) / 9;
/* K&R has this correctly done: */
celsius = (5.0/9.0) * (fahr-32.0);
> printf("%d\t%d\n", fahr, celsius);
/* K&R has this correctly done: */
printf("%4.0f %6.1f\n", fahr, celsius);
> fahr = fahr + step;
> }
> }

Are you and Sather really claiming that K&R changed a working program in
K&R1 to a grossly broken one in K&R2?
 
P

Peter Nilsson

Martin Ambuhl said:
Please cite the page on which you claim to have found this program as
Sather posted it.

Page 9 on my copy. Section 1.2 Variables and Arithmetic Expressions
If that is not a typing error, then either

are typing errors, or the program is presented as an example of a broken
program.

Martin, the posted program is fine as far as C90 is concerned (AFAICS).

You could quible over the decimal accuracy, but it's really irrelevant in
such a toy program. [Whether I reheat my pizza at 148 or at 149 degrees C
won't make _too_ much difference! ;-]
As it happens, the program on page of K&R1 (1978) has none of those
three lines as shown in the posting by Mark Sather. On the left is the
code Sather posted; indented is the code from K&R1 when it differs:

/* Sather's posting */

/* K&R has this declarion below that for lower, upper, step,
and has it as: */
float fahr, celsius;

K&R2 has that (and other) modifications in a separate example on page 12.
Are you and Sather really claiming that K&R changed a working program
in K&R1 to a grossly broken one in K&R2?

So far, _you're_ the only one claiming there's anything wrong with the code!
;)
 
M

Martin Ambuhl

Peter said:
So far, _you're_ the only one claiming there's anything wrong with the code!
;)

I never claimed the code was in any way not valid C, which seems to be
your hangup. The original poster only told us it didn't work. It
obviously does *not* work for the purpose of converting temperatures.
Nor does it correspond, as he and one other person claimed, to the code
in K&R. He failed to type the program in as it appears, and the result
is that it doesn't work. Idiot.
 
I

Irrwahn Grausewitz

Martin Ambuhl said:
I never claimed the code was in any way not valid C, which seems to be
your hangup. The original poster only told us it didn't work. It
obviously does *not* work for the purpose of converting temperatures.
Nor does it correspond, as he and one other person claimed, to the code
in K&R. He failed to type the program in as it appears, and the result
is that it doesn't work. Idiot.

Before calling other people idiots you certainly want to point out
the differences (modulo comments and whitespace) between the following
two programs, the first of which was cut'n'pasted from OP's message,
the second one being an exact copy of the _first_ example in section
1.2 of K&R2:

---8<-------OP-------------------------------------------------

#include <stdio.h>

main()
{
int fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}

---8<-------K&R2-----------------------------------------------

#include <stdio.h>

/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature scale */
upper = 300; /* upper limit */
step = 20; /* step size */

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}

---8<--------------------------------------------------------

Please report back if you find any substantial differences.

HTH
Regards

PS: I no longer own a working copy of K&R1 to cross-check, but as the
OP claimed he just bought the book, it's _extremely_ unlikely he's
holding anything else but a copy the second edition.
 
I

Irrwahn Grausewitz

JaSeong Ju said:
I copied and pasted the posted code and compiled
fine. No errors but there were 2 warnings.
Also since you're computing the
values, the variables should be either float or double.

Which is _exactly_ what the *second*, improved example in the
very same section of the book shows.

Regards
 
D

Dan Pop

In said:
I never claimed the code was in any way not valid C, which seems to be
your hangup. The original poster only told us it didn't work. It
obviously does *not* work for the purpose of converting temperatures.
Nor does it correspond, as he and one other person claimed, to the code
in K&R. He failed to type the program in as it appears, and the result
is that it doesn't work. Idiot.

I'm sorry, but the only idiot around is you. The code posted by the OP
is a verbatim copy of the program on page 9 in K&R2 (except that the
comments are stripped). If compiled and executed it produces the same
results as those listed on page 8 of K&R2. If you have *any* comments
about its correctness, please address them to Messrs Kernighan & Ritchie.

The K&R1 program you are talking about also exists in K&R2, at page 12,
as a refinement of the program at page 9, but this is, OBVIOUSLY, not the
program the OP was referring to.

The OP's problem is, most likely, that he doesn't know how to install or
how to use his compilers.

Dan
 
D

Dan Pop

In said:
I just bought The C Programming Language, by the guy who made C and
so...I have 2 different compilers, free ones, and I don't know if I
typed the code in wrong or if it's a problem with my compilers, do any
of you know? They were free compilers: Icc-win32 and Bloodshed Dev
C++(uses C too). Are there and compilers that are free besides mars,
and gcc-whatever, can't get them to work. Thanks.

heres the code, is there anything wrong with it:

#include <stdio.h>

main()
{
int fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}

If I compile and execute the code you posted, I get exactly the same
results as those listed on page 8 of your book. So, you may want to
provide a proper description of the problems you're currently having with
it.

Dan
 
J

jacob navia

I compiled the code he posted with lcc-win32 using:
lc kr.c
kr
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148

It works with lcc-win32, as far as I can tell
The problem is that the OP doesn't know what he is doing
 
D

Dan Pop

In said:
I compiled the code he posted with lcc-win32 using:
lc kr.c
kr
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148

It works with lcc-win32, as far as I can tell

Relax, no one suspected lcc-win32 of being unable to cope with this
program.
The problem is that the OP doesn't know what he is doing

The OP is an *absolute* beginner (he's playing with a program from page 9
in K&R2). It would be highly unreasonable to expect him to know what he
is doing...

Are you sure you have the right kind of documentation for such users and
that it is trivially easy for them to find it? Yes, I could find the
documentation of the command line tools in no time at all, but I knew
in advance what to look for. The absolute newbie, by virtue of being
an absolute newbie, doesn't, and you can't realistically expect him to
read everything until he discovers the good (for him) stuff.

So, you may want to consider a very short tutorial of how to use the
command line tools, for the absolute beginner. The IDE is not the best
idea for such people, because they are confronted with too many things
they don't understand at once, while a "lc myprog.c" that compiles and
links myprog.c, producing myprog.exe (or a bunch of error messages) is
exactly what they need.

Dan
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top