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".
Related
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.
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What's really happening behind?
int a=10,b=5,c=3;
b!=!a;
c=!!a;
Why the values of b and c are 5 and 1 respectively?
b is 5 because you've assigned it that value and never changed it, since
b!=!a;
...is just a condition that you don't do anything with, not any form of assignment.
c is 1 because a is 10 and !10 is 0, and !0 is 1, thus !!a is 1 (a is 10).
You already have got the answer to your question, but just to have a deeper look into why it happens that way, let me add my two cents here.
An expression like
b!=!a;
is same as
b != !a;
because of the maximal munch rule used in the translation phase. Basically, this says that the longest possible token should be selected from the input while creating a construct (obviously, valid/ meaningful).
Following this principle the ! and = are considered together to form the perfectly valid operator != and the expression is parsed like
b != !a;
over
b ! = !a; //or anything else.
That is why, there is no assignment, as you might have thought.
That said, ! is an unary operator, which does not change the value of the operand. So, taken together, your code is essentially same as
int a=10,b=5,c=3;
c=!!a; //double negation
so, a and b are unchanged, and c is 1 (because !!10 == !0 == 1 )
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 6 years ago.
Improve this question
I am expecting a to be 5, making the comparison expression evaluate to true and thus print:
A = 5, B = 3, C = 6
main()
{
int i,a,b=3,c=6,s;
for(i=1;i<=1000;i++){
if(a*a+b*b==c*c){
printf("A = %d B = %d C = %d\n",a,b,c);
}
a = i;
}
}
Where is a initialized? I am seeing i being set, as well as b and c, but I'm not seeing any assignment to a.
The solution is to assign the current value of i to a: a=i prior to the if statement. You are assigning the value after the fact. Alternatively, evaluate i, not a.
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
Spot all bugs in the code snippet below
uint arr[100]
for (uint i=99; i >=0; i--)
arr[i] = 0;
This is a question for the test, can anybody point me to all bugs in this snippet
uint isn't a type.
The first line is missing a semicolon.
i >= 0 is always true.
arr[0U - 1] is undefined behavior because it access outside the bounds of the arr array.
It's not clear that this snippet is running as part of a function. If it is not, then the entire for-loop is a syntax error.
Additionally, if this class is taking place before 1999, then:
You can't declare variables in a for loop. Instead, the uint i should be declared before the loop.
This code should probably be rewritten as simply:
unsigned arr[100] = {0};