Adee said:
Well yesterday I came across a strange behaviour of C program i.e.
let suppose we have three integers
int a=5, b=6, c=7;
if(c>b , b>a)
printf("C is greater %d",c);
this if statment is working, I am surprised about it. Can anyone
explain this feature to me. Thanks
Where did you come across this? Did you write the code yourself? What
exactly did you expect it to do?
C makes it very easy to make mistakes that radically alter the meaning
of a piece of code without making it illegal.
We can speculate that what you really wanted to write was:
if (c > b && b > a)
but we can't be sure of that if you don't tell us.
We can tell you exactly what a given chunk of C code actually means,
but that's only part of the picture. A programmer's task more
commonly starts with something you want to do, and asks what code
to write to accomplish it. If your goal is to learn the language
rather than to solve random little puzzles, we can be far more
helpful if you provide us with more context.
For example, if my guess about what you were trying to do is correct,
you might have asked:
Given
int a=5, b=6, c=7;
I want to test whether the value of b is between the values of a
and c. I was surprised to find that this:
if (c > b, b > a)
actually works. Can you explain why?
We could have told you both why your code behaves the way it does, and
what you should have written.
But I'm still not sure what you were really trying to accomplish. Only
you can tell us that.
The more context you can give us, the better job we can do of both
answering your direct question and giving you the knowledge that
will let you answer the next question yourself.
Also, the output of your printf call:
printf("C is greater %d",c);
will be:
C is greater 7
which is confusing (even ignoring that "c" and "C" are two different
identifiers).