i'm not even sure if comma operator is the problem here but i didn't want to write "Weird problem with my C code.
#include <stdio.h>
int main() {
int i = 0, x = 0, y = 0, z = 0;
y = 3;
z = 5;
(x&&(y = z), i = 10);
printf("i:%d, x:%d, y:%d, z:%d\n", x, y, z, i);
return 0;
}
Compiled with:
$ gcc -Wall deneme.c -o deneme
deneme.c: In function ‘main’:
deneme.c:9:4: warning: value computed is not used [-Wunused-value]
deneme.c:9:4: warning: value computed is not used [-Wunused-value]
and output is:
i:0, x:3, y:5, z:10
So, here is my questions:
Why i is not 10?
y is 5 and that's okay, but why z is 10?
Why x is 3?
Sorry if it's an easy question but i'm totally confused.
printf("i:%d, x:%d, y:%d, z:%d\n", x, y, z, i);
You printed x as i, y as x, z as y and i as z.
You are specifying wrong order of the arguments in the printf
Um... the order of your arguments to the printf doesn't match the text of the string.
Ie: you print i x y z, but you pass in x y z i.
So i=10, x=0, y=3 z= 5.
The reason for the warnings is not to do with the the comma operator, but rather this sub-expression: x && (y = z) is evaluated but not assigned to anything or used in any way, so the result is discarded.
It does have a side effect that may be intentional by the short circuit evaluation of x. i.e. if x is zero, y will not be assigned z, but that would be funky code best avoided.
In fact the comma operator is also best avoided outside of <expr1> or <expr3> of a `for( ; ; ) construct perhaps. There are few useful or compelling uses for it otherwise.
Related
I have to analyze what some code in C does, and I have a doubt about what happens in a certain line. The code is
#define PRINTX printf("%d\n", x)
void problem() {
int x = 2, y, z;
x *= 3 + 2; PRINTX;
x *= y = z = 4; PRINTX;
x = y == z; PRINTX;
x == (y = z); PRINTX; // This line is the problem
}
This code snippet prints the resulting numbers:
10
40
1
1 // This result **
the problem is that I'm still trying to figure out why does the last line prints out x = 1, when the operation is x == (y = z). I'm having trouble finding out what that 1 means and the precedence of the operations. Hope someone can help me! :)
Nothing in the last statement changes the value of x, so its value remains unchanged.
Parens were used to override precedence, forcing the = to be the operand of the ==.
An operator's operands must necessarily be evaluated before the operator itself, so we know the following:
y is evaluated at some point before the =.
z is evaluated at some point before the =.
x is evaluated at some point before the ==.
= is evaluated at some point before ==.
That's it. All of these are valid orders:
z y = x ==
y z = x ==
x y z = ==
etc.
But whenever x, y and z are evaluated, we can count on the following happening:
= assigns the value of z (currently 4) to y and returns it.
== compares the value of x (currently 1) with the value returned by = (4). Since they're different, == returns 0 (which isn't used by anything).
As you see, nothing changed x, so it still has the value it previously had (1).
In the last statement, nothing is changing the value of x. We are testing if x equals something, but we aren't changing it's value.
So it continues having the same value as it had in the previous statement, in particular, a value of 1.
the reason is because the == operator checks if the 2 numbers are equal, and returns 1 if equal and 0 if not equal that is why it returns one you can check by making x= 1 and y=2 and using the == operator between them
The comparison result of x and assignment of y with (y = z) is discarded. Last line could have dropped the compare: y = z; PRINTX;.
The assignment is not subsequently used either, so the line could have been PRINTX;.
I'm first year student in Software Engineering and in my last task I used (chain assignment) multiple assignment of values.
The lecturer claims that it is illegal to assign values in this way.
X = Y = Z;
(The three variables are declared in the beginning of the function).
I would be happy if you can explain to me if the assign is right action and if what the teacher claims is right.
It depends on whether all of the objects were correctly declared and Z was initialized or assigned with an appropriate value before this statement or not.
You can even assign values of objects of different types due to implicit casting, but the object which is getting assigned needs to be capable of holding the value of the object which is assigned to this one.
If Z was initialized or assigned before and all objects were correct declared, then:
X = Y = Z;
is completely correct and legal as it assigns the value held in Z to Y and X - the assignment takes place from right to left.
For example:
int X,Y,Z; // All objects are declared.
Z = 24; // `Z` is assigned with the integer value 24.
X = Y = Z; // `X` and `Y` get assigned by 24 which is the value in `Z`.
Else if Z has an indeterminate value (f.e. if it was not declared with the extern or static keyword or declared at global scope) and you would assign this value to Y and X with that statement.
Although the assignment itself would be even then not illegal, if you would use one of the assigned objects the program would give undefined results/output.
Speaking for the multiple assignments only, there is nothing illegal about it, you could even use:
O = P = Q = R = S = T = U = V = W = X = Y = Z;
if all objects used were correctly declared before and Z has an determined value.
The lecturer claims that it is illegal to assign values in this way.
If it is possible, I would ask her/him or your teacher, what s/he meant with that. Maybe there is something specific to your course you should care about.
Almost every expression has a value.
An assignment is an expression with value.
The value of the expression i = 42 is 42.
Assignment associates right to left, so the expression a = b = c is the same as a = (b = c).
So
int i;
double d;
char c;
c = i = d = 42;
Converts 42 to double and assigns to d. The value of this assignment is 42.0.
Then this value is converted to int and assigned to i. Value of this assignment is 42.
Lastly the int value is converted to char and assigned to c.
c = (i = (d = 42));
It's legal, for example:
float x;
int y;
float z = 3.5;
x = y = z;
it's similar to y = z; x = y;. So in this case, z = 3.5, y = 3 and x = 3;
But it will give the warning if you did not initialize the value of z.
int x,y, z;
x = 0;
y = 1;
//z = 2;
x = y = z;
Together with right-associativity of evaluation, and assuming non-volatile a, b, and c, it means that a = b = c is equivalent to a = (b = c), and again equivalent to b = c; a = b
Assigning values to multiple variables in a single line is perfectly alright and is allowed by C programming language.
However, the use of this style is usually discouraged because it may cause undesirable side-effects in some cases.
The expectation of Mathematical equation X = Y = Z may be:
Y = Z and X = Z (I.E., Assign the value of Z to both X and Y)
But C language treats the multiple assignments like a chain, like this:
In C, "X = Y = Z" means that the value of Z must be first assigned to Y, and then the value of Y must be assigned to X.
Here is a sample program to see how the multiple assignments in single line work:
#include <stdio.h>
int main() {
int n;
int X, Y, Z;
printf("Enter an integer: ");
scanf("%d", &n);
printf("You entered: %d\n", n);
printf("Performing multiple assignments:\n X = Y = Z = n++ \n");
X = Y = Z = n++;
printf("n = %d\n", n);
printf("X = %d \n", X);
printf("Y = %d \n", Y);
printf("Z = %d \n", Z);
return 0;
}
Output:
Enter an integer: 100
You entered: 100
Performing multiple assignments:
X = Y = Z = n++
n = 101
X = 100
Y = 100
Z = 100
Points to note in the above example:
The line X = Y = Z = n++ got processed like this:
Step 1: Assign the value of n (100) to Z
Step 2: Increment the value of n (n becomes 101)
Step 3: Assign the value of Z to Y (Y becomes 100)
Step 4: Assign the value of Y to X (X becomes 100)
Conclusion:
'Multiple assignments in single line' is a supported style. It has its benefits.
However, if the programmer is not aware of the sequence of operations involved in the multiple assignments statement, then it can lead to inconsistency between the expectation of the person who reads the program and the actual result of the execution.
To avoid this scenario, the multiple assignments are discouraged.
There's nothing illegal about it, provided each variable is only modified once. For example, the following all have well-defined behavior:
x = y = z;
x = y = z = x + 1;
x = y++ + --z;
x += (y = (z += 1)) - 6;
But the following have undefined behavior, and should be avoided:
x = y = x = z;
x = x++;
x = y++ + y++
x = func(y++, y++);
I recently learnt about undefined behaviour in C, but this particular code was used in a site as an example for 'comma as an operator', and while I understand how y = x++ in line 2, I dont understand in what order the sub expressions in line 2 are evaluated. I think it is undefined behaviour, but I'm not sure,because the site didn't mention anything as such.
int main()
{
int x = 10, y;
y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++);
printf("y = %d\n", y);
printf("x = %d\n", x);
return 0;
}
Output:
x = 11
x = 12
y = 12
x = 13
It is not undefined behaviour.
You first increase x to 11, the print it, then increase it to 12 and print it, then increase it after evaluation, so x will be 13 and the whole expression will evaluate to 12.
This is caused due to the comma operator in C being a sequence point, which means it is guaranteed all side effects of previous evaluations will have been performed, and no side effect from subsequent evaluations have yet been performed.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on that code, and I tried to use x + 1 instead of ++x and the result was infinite loop and zeros were printed on the screen.
this is the code:
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x + 1) //instead of ++x
printf("%d\t",x);
return 0;
}
I wondered this action.....why the compiler didn't produce such an error,,,, and what actually happens??!! and is x++ is treated as x += 1 ?!
You need the value of x to change, or your loop will never terminate. When you have only x + 1, then at the end of the iteration, x + 1 is computed, but its resulting value is discarded. The result of the expression is not stored any place. So x will remain zero, and x <= 100 will remain true, and your loop will continue forever.
If you have any of the following:
x = x + 1
x += 1
x++
++x
the value of x itself is incremented. That's what you need.
Although the basic structure of a for loop is like-
for(initialization; condition; increment or decrement)
But in its core part the "condition" part is treated only to control the loop behavior. So, if other two parts are syntactically correct, then compiler won't produce any error.
Since, x+1 is a valid C statement and the value of the x is not changing so it will go to an infinite loop for the condition [x <= 100 => 0 <= 100] being true forever.
Again, x++ or ++x both treated as x = x + 1 when used independently. But, x++ is actually Post-increment operator, while ++x is Pre-increment operator. That means, the with ++x, the value of the x will be incremented first and then assigned to x. Whereas, the current value of x will be used for the entire statement in x++ and then x will be incremented and assigned with new value. Look at the following example-
#include<stdio.h>
void main()
{
int x=5;
/* Post & Pre-Increment used independently */
x++;
printf("x : %d", x);
++x;
printf("\nx : %d", x);
/* Used with printf() statement */
printf("\nPre-increment of x: %d", ++x);
printf("\nPost-increment of x: %d", x++);
printf("\nPost-increment effect on x: %d", x);
}
Output:
x : 6
x : 7
Pre-increment of x: 8
Post-increment of x: 8
Post-increment effect on x: 9
I Hope my explanation have made you understand, if still not you can reply me back.
The expression x + 1 should be x = x + 1 in for loop, so correct:
for(x = 0; x <= 100; x + 1)
^
doesn't increment x
Infinite loop!
as:
for(x = 0; x <= 100; x = x + 1)
or
for(x = 0; x <= 100; x += 1) // or simply x++ or ++x
The compiler didn't produce such an error because x + 1 is a valid expression (but its not what you wants).
So, x += 1, x++ or ++x are not just add one to x but also modify value of x.
Your approach is not perfect because it definitely increases the value of x but when it comes to assignment, it fails. so your statements should cover all your requirements.
try to use
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x = x + 1)
printf("%d\t",x);
return 0;
}
or you can use
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x++)
printf("%d\t",x);
return 0;
}
Instead of x++ (post increment) you can also use pre increment (++x).
The expression x = x + 1; can also be written as x += 1; which is a shorthand method for assignment embedded with increment. It is not just about incremnent you can also use other operators.
x -= 3;
x *= 2;
#include <stdio.h>
int main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d %d %d", x, y, z);
}
Output: 2 3 3
Can anyone explain this?
And what does i =+ j mean (suppose i = 1 and j = 2)?
y = --x means "decrease x by one, then store the result in y"
z = x-- means "save a temp of x. Decrease x by one. Store the temp value in z"
Hence:
x starts at 4.
It gets decreased by 1 (to 3). 3 is stored in y.
x is saved to a temp. x is decreased again (to 2). then the temp (3) is stored in z.
y and z are printed as 3, x is printed as 2.
The postfix decrement operator (x--) returns the value of the variable before it was decremented.
x = 2, because you've decremented it
twice.
y = 3, because you've assigned
it to the value of x after it was
decremented from 4
z = 3, because you've
assigned it to the value of x
before it was decremented from 3.
simple explanation:
--x or ++x : Value will be modified after.
x-- or x++ : Value will be modified before
Detailed explanation:
--x or ++x: pre-decrement/increment: will first do the operation of decrementing or incrementing first, then it will assign x.
x-- or x++: post:decrement/increment: will first assign the value of x and then it will do the operation of decrementing or incrementing after.
lets write your code in a nicer format and go through your code step by step and annotate it to show you visually what happens:
main() {
//We declare the variables x, y and z, only x is given a value of 4.
int x=4,y,z;
//--x will decrement var x by 1 first THEN it will assign the value of x to y.
//so: x = 3, y = 3 and z = nothing yet.
y = --x;
//x-- will assign the value of x to z first, then var x will be decremented by 1 after.
//so: x = 2, y=3 and z = 3
z = x--;
printf ("\n %d %d %d", x,y,z);
}
You have to understand the notions of post-decrement and pre-decrement operator.
Both will decrement your variable, but one of them will return the original value (x--) and one of them will return the decremented value (--x).
Postfix decrement (x--) is different from prefix decrement (--x).
The former return the value x, then decrements it; the latter decrements and then returns the value.
And if you thing how a postfix is written at low level, you'll find that it is a liiiitle slower than the prefix... :)
y = --x;
X is decremented, then Y is assigned with the value of X (3)
z = x--;
Z is assigned with the value of X (3), the X is decremented (2)
Yes:
x = 4
y = pre-decrement x (basically decrement by 1 and then assign, i.e. y = x-1 = 3
x =3
z = post-decrement x (decrement by 1 after assigning the value, i.e. z = x = 3, then x = x - 1
x = 2
So x = 2, y = 3, z = 3, exactly what you saw.
If the operator is a prefix the incrementation happens before the assignment, if the operator is a postfix the incrementation happens after the assignment.
let ** be an increment/decrement operator. **e means apply ** to e and evaluate the result whereas e** means evaluate e and then apply ** to it.
Ergo, if decrementation and evaluation are seperated, the code reads as:
int x=4,y,z;
x-=1;//3
y = x;//3
z = x;//3
x-=1;//2
which gives you the ouput you have ;)
x++ / x-- is called post increment / decrement (value modified after...)
++x / ++x is called pre increment / decrement (value modified before...)
Here is what happens (roughly) in your example :
int x=4,y,z; // declare x=4, y=0, z=0
y = --x; // decrement x then assign it to y (pre decrement) (y=x=3)
z = x--; // assign x to z then decrement x (post decrement) (z=3 and x=2)
printf ("\n %d %d %d", x,y,z); // output 2 3 3
A pre increment/decrement looks like this in pseudocode
read value
increment/decrement value
write value
assign value
and a post increment/decrement looks like this
read value
assign value
increment/decrement value
write value
#include<stdio.h>
main ()
{
int x=4,y,z;
y = --x;
z = x--;
printf ("\n %d %d %d", x,y,z);
}
output 2,3,3.................................first time x=4 fine. y=--x, means value of x is decremented by 1 and stored in y, thus now y=3 and x is also 3. then z=x-- means value of x is stored in z( z=3) and then x is decremented i.e now x=2 but z=3. when u r printing the value, then printf() prints 2 3 3
Talking about what i=+j; means(given i=1 and j=2)
i=+j; is equivalent to i=i+j; so your expression becomes i=1+2 i.e. i=3