X, Y, XY why does this work in gcc? [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Why does this code work?
#include <stdio.h>
#define X 1
#define Y 2
int main(){
int XY = XY;
printf("%d\n", XY);
return 0;
}
It prints a garbage value and no errors.
I suppose this is UB?
I tested this code on mac OS, brew gcc 7.1.0.
It seems other users of gcc are experiencing similar results.

It's undefined behavior to use the value of an uninitialized (auto) variable that could have been declared with the register keyword (see 6.3.2.1p2 in the C standard).
int XY; could have been declared with register (you're not taking its address anywhere) and it's still unitialized at the right hand side of int XY = XY;, so the behavior is undefined.
If you did int XY = *&XY; the behavior would no longer be undefined, but XY would get an indeterminate value.

Related

Can static variable be re-initialised [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In the main() function,
i,a and b are decalred static. Ok, fair enough.
The function is called.
All i,a and b have some value. Ok, fair enough.
The function is called again.
Now as a static variable i , a and b must retain their value.
But
How is i again intialised to 0? (Shouldnt it contain its previous value?)
Snap shot of the problem.
P.s Answer is d btw.
i is not initialized again:
void printtab()
{
static int i, a = -3, b = -6;
i = 0;
...
It is assigned a new value when the function is entered.

Variable enters C function as nonzero, turns to zero inside by itself [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm debugging a piece of code and found that when I print out a variable prior to function execution it is nonzero. The same variable is then used as an input argument for a function, but when printing it out right at the beginning of that function it says it's zero.
Outside the function call it looks like this:
printf("sigma_b_m: %f \n",sigma_b_m);
oprq_init(sigma_b_m, b_m, r_m, x, P);
Inside the function call I did the following:
void oprq_init(const float sigma_b_m, const float b[3], const float r[3], float K[16], float P[256]) {`
printf("sigma_b_m: %f \n",sigma_b_m);
}
I removed the rest of the function for clarity but the printf line is really at the very start of the function.
The output is then:
sigma_b_m: 0.001745
sigma_b_m: 0.000000
Any idea how it now registers as zero rather than the 0.001745 it told me it was prior to going into the function?
Ok I realized my own dumb mistake... the header file had a typo in the #ifndef statement... I'm sorry for the trouble guys and my obstinacy.

I wrote a C code for solving heat equation, but at the execution it says "Segmentation fault (core dumped)".so can someone help me with this? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include<stdio.h>
#include<math.h>
#define L 10
#define T 10
#define Kmax 1000
#define N 1000
#define cond 0.25
int main() {
int i,j;
float dt,dx,b;
float x[i],t[j],u[i][j]; b=2*cond*dt/(dx*dx);
for(i=1;i<=N+1;i++) { x[i]=i*dx;
u[i][T]=1; } for(j=1;j<=Kmax+1;j++) { t[j]=j*dt;
u[L][j]=0;
u[N+1][j]=0;
t[j]=(j)*dt; } for(j=1;j<Kmax;j++) { for(i=2;i<N;i++) {
u[i][j+1]=u[i][j]+0.5*b*(u[i-1][j]+u[i+1][j]-2*u[i][j]);
printf("%7.4f\t",u[i][j]);
-----
printf("\n"); } } }
b=2*cond*dt/(dx*dx);
Variables dt and dx are uninitialized and using uninitialized variables leads to undefined behavior.
You have a VLA based on variables i and j and they are uninitialized too.
Since i and j are uninitialized and you are using them to create an array looks like the value of i and j is some hugh value and there is no enough stack memory to allocate that array and you are seeing a crash.

Why is the result of this short C program "3 2"? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is the source code:
#include <stdio.h>
enum coordinate_type{ RECTANGULAR = 1,POLAR };
int main(void)
{
int RECTANGULAR;
printf("%d %d\n",RECTANGULAR,POLAR);
return 0;
}
Why is the result the following:
3 2
You are redefining what RECTANGULAR is in the main function. It gets initialized with a "random" value, in this case it is 3, but it could be anything else.
POLAR keps its value of 2 because of how the enum is defined.
Try redefining the RECTANGULAR variable in main to see different outputs.

Please tell me answer for this code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this code troubling me for a while.The expression at Line 7 is troubling me. Is it giving 0 or -1. If its 0 then answer is 2 else answer is 4.
/* How to find value of c in Line 7 expression */
#include<stdio.h>
int main()
{
int a,b,c=1;
a=b=c; // a,b,c have equal value
c=b+=a=-c; // what will be the output of this expression?
c=-c;
c=(++c)*2;
printf(“%d”,c);
return 0;
}
In C the pre-increment (decrement) and the post-increment (decrement) operators requires an L-value expression as operand. Providing an R-value or a const qualified variable results in compilation error.
An lvalue is a value that can be assigned to.
/* what is value of c */
That's easy to answer: Your question does not make any sense.
Since your code won't compile, c won't have a "value".

Resources