This question already has answers here:
C comma operator
(4 answers)
Closed 7 years ago.
int a=3,1;
int b=(5,4);
I am a beginner in c and I noticed in a book this type of initialization . what does this initialisation mean?
int b = (5,4) will first evaluate 5 then 4. The last thing that is evaluated will be assigned to the variable. For example
int b = (5,4,3,2,1)
in this case the value of b will be 1.
Related
This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Why is a = (a+b) - (b=a) a bad choice for swapping two integers?
(10 answers)
Closed 11 months ago.
#include<stdio.h>
int main()
{
int y=0;
int z;
z=(--y)+(y=20);
printf("%d",z);
return 0;
}
Why is the precedence considered from right to left in spite of parentheses?
This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
C Unsigned int providing a negative value?
(3 answers)
Closed 2 years ago.
There is an interview question in C as below.
int main()
{
unsigned int a = 9;
a = ~a;
printf("%d\n", a);
}
I though it was supposed to be 6 but it is -10.
~a is assinged back to an unsigned integer then printed out.
It should not be a negative value.
Isn't it? Hoe come?
This question already has answers here:
Shortcircuiting of AND in case of increment / decrement operator
(6 answers)
Short circuit evaluation with both && || operator
(2 answers)
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
Why post-decrements operator in the following C code is not working as desired? (to have the value of 7). Knowing that it is not an undefined behaviour.
#include<stdio.h>
int main()
{
int a = 8, r;
r = (a==8) || (a--);
printf("a = %d\n", a);
return 0;
}
In the expression (a==8) || (a--); since (a==8) is already true hence rest of the OR condition is not evaluated.
This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 4 years ago.
Here are we use typecasting from pointer to integer, but output of arithmetic operation are different from expected answer, Why?
Source Code :
int main(){
int *p,*q;
p = 1000;
q = 2000;
printf("%d",q-p);
return 0;
}
Output: 250
The same reason due to which p++ will give you 1004. Since sizeof(int) on your machine is 4, hence each operation is shown wrt sizeof(int), even the difference i.e. 1000/4.
This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 8 years ago.
The following code prints 7. It increments value of a, but b remains unaffected. Post increment operator is not supposed to increment first and then use the value which it has done in this case. It should be the other way around. Why this change in behaviour? Also, and perhaps more importantly, why is b not pre-incremented?
int main() {
int a=5, b=2;
printf("%d", a+++b);
}
You have a well defined behavior. Where
a++ + b = 5 +2 = 7
If you have a pre-incrementor operator like
printf("%d\n",++a+b);
Then the output would be 8
The ++ operator has higher precendence than the + unary plus operator so the evaluation from left to right of
a+++b will happen
a++ + b which will fetch you 7
Post-incrementation is done by taking the value of your variable first which in this case is 5 so
a= 5 and b=2
In your code a is added to b and then it is incremented