k&r2

B

Bill Cunningham

I copied this code from page 9 k&r 2 and it gives an error that even I
can see. Celsius is uninialized.

/* code */

#include <stdio.h>

main(){
int fahr,celsius,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,celsuis;
fahr=farh+step;}}

/*end*/

Celsius is intialized within the body of the while function. Why is this
code from k&r2 not working?

Bill
 
W

Walter Roberson

I copied this code from page 9 k&r 2 and it gives an error that even I
can see. Celsius is uninialized.
/* code */
#include <stdio.h>

main(){
int fahr,celsius,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,celsuis;

Did you notice the misspelling of celsius on the above line?
 
M

Marco Manfredini

Bill said:
I copied this code from page 9 k&r 2 and it gives an error that even I
can see. Celsius is uninialized.

/* code */

#include <stdio.h>

main(){
int fahr,celsius,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,celsuis;
fahr=farh+step;}}

/*end*/

Celsius is intialized within the body of the while function. Why is this
code from k&r2 not working?

You should read the message again. Isn't it rather "celsuis is
undefined"? celsuis?

And did you get something about "farh"?
 
C

CBFalconer

Bill said:
I copied this code from page 9 k&r 2 and it gives an error that
even I can see. Celsius is uninialized.

/* code */

#include <stdio.h>

main(){
int fahr,celsius,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,celsuis;
fahr=farh+step;}}

/*end*/

Celsius is intialized within the body of the while function. Why
is this code from k&r2 not working?

Because that is not the code from K&R.
 
J

J. J. Farrell

Bill said:
I copied this code from page 9 k&r 2 and it gives an error that even I
can see. Celsius is uninialized.

/* code */

#include <stdio.h>

main(){
int fahr,celsius,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,celsuis;
fahr=farh+step;}}

/*end*/

Celsius is intialized within the body of the while function. Why is this
code from k&r2 not working?

Most likely because of various typos which make it very different from
the code in K&R2.
 
J

Joe Wright

Bill said:
I copied this code from page 9 k&r 2 and it gives an error that even I
can see. Celsius is uninialized.

/* code */

#include <stdio.h>

main(){
int fahr,celsius,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,celsuis;
fahr=farh+step;}}

/*end*/

Celsius is intialized within the body of the while function. Why is this
code from k&r2 not working?

Bill
Mine works..

#include <stdio.h>

int main(void)
{
int fahr, celcius;
int lower, upper, step;

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

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

TerryP

Bill said:
No. My mistake then. :(

The Typo, the bane of compilation!

I remember I once spent 2 hours trying to find the problem in a program (of
mine) that failed to build, some times I wish they made a compiler that gave
errors like:

file:line !!! PEBKAC !!!
sp,member != sp.member -- you are a blind idiot,

After finally finding out that I had been working to long to notice the
difference between , and . I decided to get a better font xD
 
M

Martin Ambuhl

Bill said:
I copied this code from page 9 k&r 2 and it gives an error that even I
can see. Celsius is uninialized.
It has a value assigned before use, so what's the problem?
[...]
int fahr,celsius,lower,upper,step;
[...]
while (fahr<=upper)
{celsius=5*(fahr-32)/9; [...]
Why is this
code from k&r2 not working?

Is this really the code from K&R2? If it is, perhaps we should retreat
to K&R1, where these lines are
while )fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
 
J

Joachim Schmitz

CBFalconer said:
Because that is not the code from K&R.
Very helpful answer 8-(
Fortunatly others had been far more helpfull in pointing out the typo(s)

Bye, Jojo
 
J

Joachim Schmitz

Martin Ambuhl said:
Bill said:
I copied this code from page 9 k&r 2 and it gives an error that even
I can see. Celsius is uninialized.
It has a value assigned before use, so what's the problem?
[...]
int fahr,celsius,lower,upper,step;
[...]
while (fahr<=upper)
{celsius=5*(fahr-32)/9; [...]
Why is this code from k&r2 not working?

Is this really the code from K&R2? If it is, perhaps we should retreat to
K&R1, where these lines are
while )fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
Really? Does K&R1 have such bad typos?

After correction it gives a warning:
implicit conversion from "double" to "int": rounding, sign extension, or
loss of accuracy may result

Bye, Jojo
 
J

Joachim Schmitz

Joe Wright said:
Mine works..

#include <stdio.h>

int main(void)
{
int fahr, celcius;
int lower, upper, step;

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

fahr = lower;
while (fahr <= upper) {
celcius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celcius);
fahr = fahr + step;
}
return 0;
}
Your code might work, but it too misspells celsius.

Bye, Jojo

PS: This is something I notice immediatelly, because Celsius(weg) is the
street I live at... If I have to spell Celsius I usually refer to
Fahrenheit, once with the result that it got spelled "Felsius" :cool:
 
M

Martin Ambuhl

Joachim said:
Martin Ambuhl said:
Bill said:
I copied this code from page 9 k&r 2 and it gives an error that even
I can see. Celsius is uninialized.
It has a value assigned before use, so what's the problem?
[...]
int fahr,celsius,lower,upper,step;
[...]
while (fahr<=upper)
{celsius=5*(fahr-32)/9; [...]
Why is this code from k&r2 not working?
Is this really the code from K&R2? If it is, perhaps we should retreat to
K&R1, where these lines are
while )fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
Really? Does K&R1 have such bad typos?

No, that's my typo (and unless I missing something, that is properly
singular).
After correction it gives a warning:
implicit conversion from "double" to "int": rounding, sign extension, or
loss of accuracy may result

While your implementation is free to issue such warnings, they are
hardly required. People with a mania for compiles without warnings
might take this is an indication that they should "fix" the code with a
useless cast. Since casts to "shut the compiler up" are all too
frequently a bad idea, I'm not so sure that the warning is a good idea.
 
S

santosh

Joachim Schmitz said:
Martin Ambuhl said:
Bill said:
I copied this code from page 9 k&r 2 and it gives an error that
even
I can see. Celsius is uninialized.
It has a value assigned before use, so what's the problem?
[...]
int fahr,celsius,lower,upper,step;
[...]
while (fahr<=upper)
{celsius=5*(fahr-32)/9; [...]
Why is this code from k&r2 not working?

Is this really the code from K&R2? If it is, perhaps we should
retreat to K&R1, where these lines are
while )fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
Really? Does K&R1 have such bad typos?

After correction it gives a warning:
implicit conversion from "double" to "int": rounding, sign extension,
or loss of accuracy may result

I could be wrong but, depending on the value in 'fahr', that statement
might invoke undefined behaviour.
 
R

Richard Heathfield

Joachim Schmitz said:
Your code might work, but it too misspells celsius.

Bye, Jojo

PS: This is something I notice immediatelly,

I note that your spelling flame contains a spelling error. It's
heart-warming to see the old traditions being maintained. :)
 
J

Joachim Schmitz

Richard Heathfield said:
Joachim Schmitz said:


I note that your spelling flame contains a spelling error. It's
heart-warming to see the old traditions being maintained. :)
After all I live at Celsiusweg, not at Immediately Drive :cool:

Bye, Jojo
 
J

Joachim Schmitz

Martin Ambuhl said:
While your implementation is free to issue such warnings, they are hardly
required. People with a mania for compiles without warnings might take
this is an indication that they should "fix" the code with a useless cast.
Since casts to "shut the compiler up" are all too frequently a bad idea,
I'm not so sure that the warning is a good idea.
But in this case it tells me that using double arithmetic doesn't buy a
thing in the first place.

Bye, Jojo
 

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,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top