What does the expression (i,j) in c signifies? [duplicate] - c

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I tried executing the following c code:
#include <stdio.h>
int main()
{
int i=2000,j=100;
printf("%d",(j,i));
}
I gave different values to i and j and found the output. I always get the output as the value contained in the second variable. Does the expression always give the last variable as a result or does it have any other meaning?

You are using the comma operator.
The output of your code is 2000, because the comma operator evaluates the two expressions and returns the value of the second operand. More details can be found in this SO answer.

Related

is there any array[a,b] syntax in c? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 24 days ago.
while reading about the c preprocessor I got something like array[x=y,x+1]. I haven't seen this kind of syntax in c before and after searching for many hours I didn't find anything useful.
#include <stdio.h>
int main() {
int arr[] = {5,10,15};
printf("%d %d %d",arr[0,1]);
return 0;
}
outputs:
10 1762365112 1769491896
Can someone elaborate on this?
Because of how the comma operator (,) works in C, the effect of the (questionable and woefully hard to read) array[x=y,x+1] is to first copy the value of y into x and then use x+1 as the normal (single dimensional, non-ranged, or whatever you were expecting) index in to the array.
(I do not see the need to discuss your experimental code.)
See the link to documentation of comma operator, as kindly provided in comments by Pignotto:
https://en.cppreference.com/w/c/language/operator_other#Comma_operator

I do not understand this C notation [duplicate]

This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
Closed 10 months ago.
Can someone explain this kind of statement(s)? I didn't get it.
data[i] = ((data[i] == div[i]) ? '0' : '1');
It's an if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
So , your expression can be visualized into if-else statement as:
#include <stdio.h>
int main(){
// Code ...
if(data[i]==div[i])
data[i]='0';
else
data[i]='1';
}
** ‘?:’ takes three operands to work, hence they are also called ternary operators.

How do I calculate the output [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
#include<stdio.h>
void main()
{
int x=3,y=2,z=0,m;
m=++x || ++y && ++z;
printf("\n %d %d %d %d\n",x,y,z,m); // 4 2 0 1
}
The output of the following code is mentioned as comment in program and I am trying to evaluate how this answer came but I am not able to understand.
I just wanted to know how the program calculates the relative value.
thanks to pmg, i have corrected my original answer (i had an error)
Because the left side of the OR operator (||) is not zero, it doesn't evaluate anything else on that line. This is called a "short circuit operator". In this example you gave, the programmer basically is tricking the compiler. If the argument on the right of the operator does not affect the outcome, it will not execute that code. However in this case, there are increments going on there and they won't be evaluated either.
This will assign "1" to m.
your output should be 3, 2, 0, 1.

What a " , " does in a while loop? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
I accidentally typed while(x,0) instead of while(x<0). The code of course didn't work as planned nor got compiler errors so i took a damn hour to find the mistake.
Why it didn't get a compiler error? And what does the , do in the while loop?
while(x,0)
comma is treated as binary operator and it will return 0, so your loop condition will go false.
In C, a comma is "a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)."
Since 0 is false then your code was exiting the loop.
https://en.m.wikipedia.org/wiki/Comma_operator

how is this post increment working? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
#include <stdio.h>
int main ()
{
int a=10;
printf("%d %d %d",a,a++,a);
return 0;
}
The output I am getting is "11 10 11".
I thought the output would be "10 10 11".
why a is incrementing like this?
Because there is no guarantee about the order in which a C compiler evaluates the arguments. The only thing guaranteed (by the standard) is that they are all evaluated before doing the call. Therefore, you should never count on the order of evaluation of the arguments. Just consider it as random.
Hence, in general, avoid using auto-increment if the same variable exists more than once in an argument list.

Resources