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

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.

Related

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.

not able to understand how compiler(gcc) is interpreting the command(c) and giving output of the statement [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Can anyone help me out, I am not getting how gcc is compiling the below statement and printing its output.:-
printf("%d",7["sunderban"]);
C allows to access array's elements in two ways (see Accessing arrays by index[array] in C and C++ and the answers) :
int v[5];
// 1)
v[2] = 33;
// 2)
2[v] = 44;
So, what happens in your case is that you access the 8th element of the string, and it is interpret as int by the printf.

I keep getting this compiling error [duplicate]

This question already has an answer here:
Keep getting this compiling error [closed]
(1 answer)
Closed 9 years ago.
When I complile, I keep getting this error.
mario.c:11:14: error: expected expression
while (n=<0);
^
I've tried changing things and then fixing them and changing other things and then fixing those but nothing seems to help. I'm new at this. Can anyone help?
do
{
n = GetInt();
}
while (n=<0);
=< is invalid syntax; you want <=, like this:
do
{
n = GetInt();
}
while (n<=0);

I am getting only 2 as op for the below program [duplicate]

This question already has answers here:
Is uninitialized data behavior well specified?
(7 answers)
What will be the value of uninitialized variable? [duplicate]
(6 answers)
Closed 8 years ago.
Please explain why I am getting output 2 here. My expected o/p is 5 or 7. Please throw some light. Thank you!
#include<stdio.h>
typedef enum {a=3, b, c, d, j}e;
void f(e *e1) {
printf("%ld", (int)*e1);
}
main(){
e es;
f(&es);
}
You haven't initialized es, so your program is just printing the random value that happens to be on the stack when the program runs.
You need to say something like:
e es = c;
That will give you the 5 output you seek.

issue with the conditional operator [duplicate]

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.

Resources