473,698 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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;
}
}
Nov 14 '05 #1
14 1560

"Matt Suther" <dn******@hotma il.com> wrote in message

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?
Nov 14 '05 #2
Matt Suther wrote:
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; }

Nov 14 '05 #3
Martin Ambuhl <ma*****@earthl ink.net> wrote:
Matt Suther wrote:
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;

^^^^^^^^^^^^^^^ ^^
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
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
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.

Nov 14 '05 #5
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.

Nov 14 '05 #6
Je***********@p hysik.fu-berlin.de wrote:
Martin Ambuhl <ma*****@earthl ink.net> wrote:
Matt Suther wrote:
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
^^^^^^^^^^^^^^^ ^^
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).


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?

Nov 14 '05 #7
"Martin Ambuhl" <ma*****@earthl ink.net> wrote...
Je***********@p hysik.fu-berlin.de wrote:
Martin Ambuhl <ma*****@earthl ink.net> wrote:
Matt Suther wrote:

> while (fahr <= upper) {
> celsius = 5 * (fahr-32) / 9;
^^^^^^^^^^^^^^^ ^^
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).


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


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 */
> #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;


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!
;)

--
Peter
Nov 14 '05 #8
Peter Nilsson wrote:
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.
Nov 14 '05 #9
Martin Ambuhl <ma*****@earthl ink.net> wrote:
Peter Nilsson wrote:
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.


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.

--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #10

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

Similar topics

100
6977
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We are discussing whether this newsgroup should focus on 100% ANSI C or simply topics related to the C language in the real world. There is a C standard which is defined by an international committee. People who write compilers refer to this in...
14
1134
by: Matt Suther | last post by:
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:...
5
1411
by: celsius | last post by:
Hi all, please forgive me if this already posted many times. i was reading peter van der linden's book expert C programming. on page number 188,he is discussing about implementing finite state machine in C. he explains as follows :- there are several ways to implement finite state
5
1620
by: Mr. Olsen | last post by:
Dear Everyone, This is my first post on this newsgroup. I have extensive experience and knowledge of other programming languages, primarily on an Assembler level, the Z80, 6502/10, MC680x0, but also experience with Pascal, Basic and a multiple of other languages. I used to develop computer games for the 8-bit computers and the 16-bit computers such as Atari and Amiga. But "C" is "new" to me from a programming perspective. I want to...
4
3747
by: Luke Wu | last post by:
I am just wondering what the following terms usually mean: 1) "Standard C" 2) "K&R C" 3) "ANSI C" I am pretty sure "ANSI C" usually refers to the C89 standard, but what
0
9027
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
8861
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
6518
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
5860
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.