Please explain the fourth line in this C code [duplicate] - c

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
#include"studio.h"
void main() {
int i , j= 0, k;
k = i = 2 , j; // what's meaning of int , int ?
printf ( "%d", k );
}
can anyone please explain what's up with that int , int ..?

The comma operator allows you to evaluate a second expression. (a, b) returns the value of expression b.
However, because the = has a higher priority than the comma operator, the values of i, j and k are respectively 2, 0 and 2.

Related

Unexpected output in expression that uses macros [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 2 years ago.
My code:
#include <stdio.h>
#define PRODUCT(x) (x * x)
int main()
{
int i = 3, j, k, l;
j = PRODUCT(i + 1);
k = PRODUCT(i++);
l = PRODUCT(++i);
printf("%d %d %d %d", i, j, k, l);
return 0;
}
I am not able to comprehend why the output is:
7 7 12 49.
Is there any error in macro or some other problem?
Your code has undefined behavior, operations in i:
k=PRODUCT(i++);
l=PRODUCT(++i);
lack a sequence point.
As for:
j=PRODUCT(i+1);
It expands to i+1*i+1 which is i+i+1 which is 7. I assume it's not the expected result, in the future also include that in your question.
Your macro is incorrect. The following expression:
PRODUCT(i+1)
will expand to
(i+1 * i+1)
which is 2*i+1.
Your macro should be:
#define PRODUCT(x) ((x)*(x))
I strongly suggest you stop using macros for this sort of thing. You could easily write this as a function:
int product(int x)
{
return x * x;
}
Note that this will only work for the example I gave. If you try
PRODUCT(i++)
you will get
( (i++) * (i++) )
which invokes undefined behaviour, as this expression lacks a sequence point between the 2 increments.

Why does value of j not change? [duplicate]

This question already has answers here:
Why do logical operators in C not evaluate the entire expression when it's not necessary to?
(5 answers)
Closed 4 years ago.
In the following code why does the value of j remains zero even after the statement j=i+10.
#include <stdio.h>
int main()
{
int i = 10, j = 0;
if (i || (j=i+10))
printf("%d",j);// j=0
}
Why does value of j not change? Because i is non-zero and logical OR (||) properties is that if 1st operand is true don't check 2nd operand.
if ( i || (j=i+10))
| |
10(True) not evaluated

please help to explain the output of this c program [duplicate]

This question already has answers here:
Why the following function is called thrice
(6 answers)
Closed 5 years ago.
After executing i am getting the output as 12 6 11. please explain how this is possible
#include<stdio.h>
#define MAN(x,y) (x)>(y)?(x):(y)
int main()
{
int i = 10,j = 5,k = 0;
k = MAN(i++,++j);
printf("%d %d %d", i, j, k);
return 0;
}
The macro will expand the line
k=MAN(i++,j++)
as
k=(i++)>(j++)?(i++):(j++);
k=(i++)>(j++)?(i++), (j++) will not be evaluated.
so
i will be incremented twice and result in 12 j will be incremented once so 6 when k will be assigned i value is 11 so its value as11`.

Unary operation are sometimes suicidal.Mess up with C code [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
Code in C language.
#include<stdio.h>
#define PRODUCT(x) ( x * x * x)
int main()
{
int i =5,k;
k = PRODUCT( ++i );
printf("i is :%d ",k);
return 0;
}
My Question is why i is : 392? According to me output should be 336. (because 6 * 7 * 8 = 336)
Am I really messed up here??
Preprocessed code will have
( ++i * ++i * ++i)
which have Lack of sequence point between the two execution on same variable resulting Undefined behaviour.

What's exactly difference between i++ and ++i in C? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(C) What is the difference between ++i and i++
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%-4d”,a[i][j]);
}
printf(“\n”);
}
In the above case, you won't recognize it.
But it's essentially the following:
int x = a[i++] first reads from à afterwards, increases i
int x = a[++i] first increases i, afterwards reads from a
++i Increments $i by one, then returns $i.
i++ Returns $i, then increments $i by one.
i++ is a post-increment operator ie., the current value of i is used for the operation and the value is incremented by 1 after the operation.
++i is pr-increment operator ie., the value of i is incremented and the new value of is used in the operation.
int i = 2;
int a = ++i; // a is 3, i is 3
int b = i++; // b is 3, i is 4
++i is a pre-increment, while i++ is a post-increment.
In that situation, because the type is an int and it happens in a for-loop, nothing; there is no performance benefit to either.

Resources