issue with the conditional operator [duplicate] - c

This question already has answers here:
C conditional operator ('?') with empty second parameter [duplicate]
(6 answers)
Closed 9 years ago.
#include<stdio.h>
int main()
{
printf("%d\n", 4 ?: 8);
}
According to the C standard this program is invalid because it is missing an expression between the ? and :.But The interesting thing is that there is when I compile the code it is printing 4.how come it will print 4 rather than showing any compile error

This is a gcc extension.
x ? : y
is equivalent to
x ? x : y
See here for detail.

Related

How do ++ and -- operators work in this example? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
In the following piece of C code, I would expect the printed output to be 5794. But when I compile it with GCC (7.5.0) the output is 5693. Why?
int main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf("%d%d",x,y);
}
this program has undefined behavior.which means you can't predict what will happen.
look for more information here
Sequence_point

Why, the output is 6 and not 7? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 3 years ago.
The following C code executes correctly but not as expected. Post increment operator here in z=z++ is creating confusion here. I may not be able to figure out silly mistake/concept, Can I have a brief explanation or some helpful link please.
#include<stdio.h>
int main()
{
int x=5,y=6,z=7;
if(x-y)
z=z++;
z=--z;
printf("%d",z);
}
You are not allowed to do z=z++; because between 2 sequence points you are not allowed to assign a variable 2 times.
This one is a full expression in which you assign z 2 times. So it can be interpreted ambigously and the result of the C abstract machine is undefined behavior.
The same for z=--z.

What should be the result of "c -= --c - c++;" in C Language? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
I am trying the following code:
#include <stdio.h>
int main()
{
int c =0;
c -= --c - c++;
printf("%d \n",c);
return 0;
}
When I compiled and run it using a online c complier (https://www.tutorialspoint.com/compile_c_online.php) the result is -1. But I expected it to be 0.
So, I try it on my local Dev C++ (Windows) and the result is 0.
Should the result be 0 ?
If so, why 2 gcc compilers (ok they are in different plataform) gives me 2 different results ?
I ve been looking for some kind of automatic flag otimization which could produce a different result but I had no success.
THIS IS UNDEFINED BEHAVIOR (3 modifications without sequence points inbetween to the same variable)

what is the output of this according to the macro definition [duplicate]

This question already has answers here:
Strange behavior of Macro-expansion
(3 answers)
Why is the return value not what I expected in this C program with macro? [duplicate]
(2 answers)
Closed 7 years ago.
#define A 1+2
#define B 4+3
int main()
{
int val = A*B;
printf("%d",val);
}
Here A and B is defined, and val is (A*B). What is the correct output?
Let's trace the expansion and calculation manually.
A*B -> 1+2*4+3 -> 1+8+3 -> 12
As a result, the output will be 12
This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation.
As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.

What i = 1, 23 ;means. [duplicate]

This question already has answers here:
Uses of C comma operator [duplicate]
(20 answers)
How does comma operator work during assignment?
(5 answers)
Closed 9 years ago.
This funny thing happened when I make a mistake .
I what write :
int i;
i = 1;
but it is
int i ;i = 1 ,23;(I guess sometime I click the middle button of my mouse) .
Then I compiled the program by gcc and it went through without any warning or error!
And after I notice that . I try int i = 1,23; , and now the compile give a error:
error: expected identifier or ‘(’ before numeric constant
So ,Why the first time compile suceessful ?
And why it give me a error the second time?
What exactly ", 23" means?
Thanks in advance.

Resources