Little program

M

mdh

Can someone look at this little program and tell me why the last line
is not compiling.

#include <stdio.h>

int main (){


int i, j, k;
i=0;
j=9;
k=-8;
printf("Given i=0, j=9 and k= -8 \n");
printf( "Then for \" if ( i) , if (j) and if (k)\"\n");
printf("the Answers are %d %d %d ", if(i), if(j), if(k) ); /**parse
error before 'if'**/


return 0;
}

It's probably something really silly, or I am missing something, which
is more likely!! but all in the quest to learn C!!!!
Thanks in advance.
 
B

Bart

mdh said:
I thought you could (page 56, K&R II);

You are thoroughly confused about the use of the if-statement. Re read
that page and the previous one very carefully.

Regards,
Bart.
 
M

mdh

Bart said:
You are thoroughly confused about the use of the if-statement.

I could give a smart answer, but I will refrain. The reason some of us
ask, is that we wish to be enlightened. One of the ways of
enlightenment is to write little things that are causing confusion. So,
to be told that I am confused is a pointless exercise.
If you look at page 56 it says:

"Since an if simply tests the numeric value of an expression, certain
coding shortcuts are possible. The most obvious is writing

if(expression)

instead of

if (expression != 0).

I was simply trying to see if the !=0 includes negative values, hence
the program, which may not work for the reasons you say, but that does
not mean one should not try and get a better understanding.

Anyway, thanks for your help.
 
J

jmcgill

Bart said:
You are thoroughly confused about the use of the if-statement. Re read
that page and the previous one very carefully.

I have a feeling he needs to back up a bit more than that. K&R2 is a
great book, but might not be the best tutorial for someone working from
scratch, on his own.
 
B

Bart

mdh said:
I could give a smart answer, but I will refrain. The reason some of us
ask, is that we wish to be enlightened. One of the ways of
enlightenment is to write little things that are causing confusion. So,
to be told that I am confused is a pointless exercise.

I was about to explain it at length, but I just realized that I'm
repeating what's written there in K&R. Since you have the book I
thought that it would be pointless, so I just suggested that you
re-read more carefully.
If you look at page 56 it says:

"Since an if simply tests the numeric value of an expression, certain
coding shortcuts are possible. The most obvious is writing

if(expression)

instead of

if (expression != 0).

I was simply trying to see if the !=0 includes negative values, hence
the program, which may not work for the reasons you say, but that does
not mean one should not try and get a better understanding.

Yes, but it also says that the general syntax is:

if (expression)
statement;
else
statement;

You can't just use the 'if' as though it were an expression. You can
only use it in a complete statement as above.

Regards,
Bart.
 
B

Bart

jmcgill said:
I have a feeling he needs to back up a bit more than that. K&R2 is a
great book, but might not be the best tutorial for someone working from
scratch, on his own.

Don't assume too much. There are languages where the if statement is
can be used as an expression, and it's quite concievable that he's
confused precicely for this reason.

Regards,
Bart.
 
M

mdh

You can't just use the 'if' as though it were an expression. You can
only use it in a complete statement as above.


Yes....I am beginning to see that!! :)

What I had missed was that presumably when an expression does not
evaluate to '0', it could also be a negative value. (I had assumed it
would have to be > '0'. This came up in a later exercise when using
recursion to express an integer as a string (exercise 4-12). It is
strange that in the process of learning, one thinks one understands a
concept until one gets to understand it better, invariably through
making a mistake.
 
J

jmcgill

/* consider this */
#include <stdio.h>
int
main (int argc, char **argv)
{
int i, j, k;
if( argc == 4 ){
i = atoi(argv[1]);
j = atoi(argv[2]);
k = atoi(argv[3]);
printf ("Given i=%d, j=%d and k=%d \n", i, j, k);
printf ("Then for \" if(i) , if(j) and if(k)\"\n");
printf ("the Answers are %d %d %d\n",
(i ? 1 : 0), (j ? 1 : 0), (k ? 1 : 0) );
} else {
printf("usage: %s i j k\n", argv[0]);
}
return 0;
}
 
J

jmcgill

mdh said:
Yes....I am beginning to see that!! :)

If you do want to use a conditional as an expression, the ternary
operator is your friend. See my previous post for an example.
 
M

mdh

jmcgill said:
/* consider this */


Thanks...
I am 2 pages away from starting pointers!!! I will keep this for later
enlightenment!!!

To both of you, thanks for your help.

BTW....K&R is certainly challenging, but even more certainly, worth it.
 
K

Keith Thompson

mdh said:
Yes....I am beginning to see that!! :)

What I had missed was that presumably when an expression does not
evaluate to '0', it could also be a negative value. (I had assumed it
would have to be > '0'. This came up in a later exercise when using
recursion to express an integer as a string (exercise 4-12). It is
strange that in the process of learning, one thinks one understands a
concept until one gets to understand it better, invariably through
making a mistake.

That's correct. In an if statement "if (foo)", the expression "foo"
is evaluated and the condition is considered false if foo is equal to
zero (which can mean different things for different types), and true
if foo has any non-zero value.

But that's not the problem with the code you posted. You're using
something like "if (foo)" in a context that requires an expression.
"if (foo)" isn't an expression; it's a fragment of a statement. (An
expression, with a semicolon added, can be a statement, but a
statement cannot in general be used as an expression.)

"if (foo)" doesn't return or yield a value; it evaluates the condition
and then affects the program's control flow based on the result.

As jmcgill points out, the conditional operator, also known as the
ternary operator (because it happens to be the only operator that
takes three operands), can give you the effect of an if/else in an
expression context. Use it sparingly; it can easily be used to write
nearly illegible code. Something like
a = (b ? c : d);
is often clearer if it's written as
if (b) {
a = c;
}
else {
a = d;
}
 
L

lovecreatesbea...

mdh said:
printf("the Answers are %d %d %d ", if(i), if(j), if(k) );

The if is a reserved keyword and in the example they are not
expressions and have no values.
 
M

mdh

Keith said:
"
That's correct. ....snip.... But that's not the problem with the code you posted. You're using
something like "if (foo)" in a context that requires an expression......


Thank you Keith.
 
C

Chief

mdh said:
Can someone look at this little program and tell me why the last line
is not compiling.

#include <stdio.h>

int main (){


int i, j, k;
i=0;
j=9;
k=-8;
printf("Given i=0, j=9 and k= -8 \n");
printf( "Then for \" if ( i) , if (j) and if (k)\"\n");
printf("the Answers are %d %d %d ", if(i), if(j), if(k) ); /**parse
error before 'if'**/


return 0;
}
You cant use "if" statemant here but you can do the same with this line
printf("the Answers are %d %d %d ", ( i!=0) , (j!=0) , (k!=0) );
 

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,785
Messages
2,569,624
Members
45,319
Latest member
LorenFlann

Latest Threads

Top