Please explain me why it behaves differently.
int main() {
int p;
p = (printf("stack"),printf("overflow"));
printf("%d",p);
return 0;
}
This gives the output as stackoverflow8. However , if I remove the paranthesis , then :
p = printf("stack"),printf("overflow"); gives the output as stackoverflow5
The Comma Operator
The comma operator has lower precedence than assignment (it has a lower precedence than any operator for that matter), so if you remove the parentheses the assignment takes place first and the result of the second expression is discarded. So...
int a = 10, b = 20;
int x = (a,b); // x == 20
int y = a,b; // y == 10
// equivalent (in terms of assignment) to
//int y = a;
Note that the third line will cause an error as it is interpreted as a re-declaration of b, i.e.:
int y = a;
int b;
I missed this at first, but it makes sense. It is no different than the initial declaration of a and b, and in this case the comma is not an operator, it is a separator.
Related
int main()
{
int a = (1,2,3);
int b = (++a, ++a, ++a);
int c= (b++, b++, b++);
printf("%d %d %d", a,b,c);
}
I am beginner in programming. I am not getting how does this program shows me output of 6 9 8.
The , used in all the three declarations
int a = (1,2,3);
int b = (++a, ++a, ++a);
int c = (b++, b++, b++);
are comma operator. It evaluates the first operand1 and discard it, then evaluates the second operand and return its value. Therefore,
int a = ((1,2), 3); // a is initialized with 3.
int b = ((++a, ++a), ++a); // b is initialized with 4+1+1 = 6.
// a is 6 by the end of the statement
int c = ((b++, b++), b++); // c is initialized with 6+1+1 = 8
// b is 9 by the end of the statement.
1 Order of evaluation is guaranteed from left to right in case of comma operator.
The code is not in any way good and nobody in their right mind would ever write it. You should not spend any time in looking at that kind of code, but I will still give an explanation.
The comma operator , means "do the left one, discard any result, do the right one and return the result. Putting the parts in parentheses doesn't have any effect on the functionality.
Written more clearly the code would be:
int a, b, c;
a = 3; // 1 and 2 have no meaning whatsoever
a++;
a++;
a++;
b = a;
b++;
b++;
c = b;
b++;
The pre- and post-increment operators have a difference in how they act and that causes the difference in values of b and c.
I am beginner in programming. I am not getting how does this program
shows me output of
Just understand comma operators and prefix ,postfix .
according to rules mentioned in links given to you
int a = (1,2,3); // a is initialized with 3 last argument .
int b = (++a, ++a, ++a); // a is incremented three time so becomes 6 and b initilized with 6 .
int c = (b++, b++, b++); // b incremented two times becomes 8 and c initialized with 8.
// b incremented once more time becomes 9
I code from a long time but I was never asked questions like this before.
main()
{
int a=5, b, c;
b=a=15;
c=a<15;
printf("%d %d",a,c);
}
What will be the values of a and c? How do we interpret '<' or '>' ?
The < operator yields 0 or 1 of type int.
It yields 1 if its left operand is less than its right operand, 0 otherwise.
In your case the left operand has the value a or 15; the right operand has the value 15. So the operator yields the value 0.
Then that 0 is assigned to c.
Your statement with extra whitespace, parenthesis, and a comment can be written as
c = (a < 15); // assign 0 or 1 to c
main()
{
int a=5, b, c;
b=a=15; // a = 15
c=a<15; // a is not < 15, a is 15, so c is 0
printf("%d %d",a,c);
}
First of all, the result of this code is undefined, since you do not supply a function prototype for printf
Second of all, using main() is further undefined since a function needs a return type and main() in particular needs to be int return type (handling of other implementations is implementation-defined.
Now assuming you correct your code to:
#include <stdio.h>
int main(void)
{
int a=5, b, c;
b=a=15;
c=a<15;
printf("%d %d",a,c);
return 0;
}
Then the output for c is 0.
This is because the < operator returns int type 0 or 1 based on if the comparison is true.
This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 8 years ago.
I was going through K&R C book and got through this --> operator in the precedence table. So, I wondered if there was a similar operator i.e., <-- and wrote the following program:
#include<stdio.h>
void main()
{
int x = 5;
while(0 <-- x)
printf("%d",x);
}
It worked perfectly fine. So why isn't <-- is not considered as an operator? (as it is not in the precedence table!) and what is it's precedence?
--> is not one operator, it is two; (post) decrement and less than. C is whitespace agnostic for the most part, so:
x --> y
/* is the same as */
x-- > y
<-- is the same idea:
x <-- y
/* is the same as */
x < --y
Perhaps you are confusing -> with -->. -> dereferences a pointer to get at a member of the type it refers to.
typedef struct
{
int x;
} foo;
int main(void)
{
foo f = {1};
foo *fp = &f;
printf("%d", fp->x);
return 0;
}
<- is simply not an operator at all.
This is not an operator but two operators: < and --. The code is identical to
#include<stdio.h>
void main()
{
int x = 5;
while(0 < --x)
printf("%d",x);
}
As I see it, that is just a "less than" < operator followed by decreasing the variable x (--). It is not one operator, but two. And -- has precedence over <.
I have read that comma operator is used to assign expression, and the right expression is supplied to lvalue.
But why does this program assign left expression to lvalue when not using parenthesis? I am using turbo c compiler.
int b=2;
int a;
a=(b+2,b*5); // prints 10 as expected
a=b+2,b*5; // prints 4 when not using parenthesis
Also the following works:
int a =(b+2,b*5);
But this generates an error:
int a =b+2,b*5; // Error
I can't understand why.
Because precedence of , operator is lower than of = one, this...
a=b+2,b*5;
... will actually be evaluated as...
a = b + 2;
b * 5;
With int i = b + 2, b * 5; is a bit different, because comma has different meaning in declaration statements, separating different declarations from each other. Consider this:
int a = 3, b = 4;
You still have comma here, but now it separates two variable assignment-on-declarations. And that's how the compiler attempts to treat that line from your example - but fails to get any meaning from b * 5 line (it's neither assignment nor declaration).
Now, int a = (b + 2, b * 5) is different: you assign a value of b + 2, b * 5 expression to a variable a of type int. The first sub-expression is discarded, leaving you just with b * 5.
#include<stdio.h>
#define x 4+1
int main()
{
int i;
i = x*x*x;
printf("%d",i);
return 0;
}
I would like to know how the expression is evaluated.
The C preprocessor will literally substitute all instances of x for 4+1, resulting in the following code:
i = 4+1*4+1*4+1;
Since * has precedence over +, this evaluates to:
i = 4+4+4+1;
and i gets the value 13.
You can also use parentheses in definition like this:
#define x (4+1)
then, this evaluates to:
i = (4+1)*(4+1)*(4+1)
the value of i is 125