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 )
Related
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 2 years ago.
Improve this question
So, the question was find the greatest number using "if" statement between three numbers. I wrote the program myself and I'm a newbie to programming so, I tried to run this program but it showed 2 errors repeatedly.
error 1: x is undefined
error 2: y is undefined
So, I understand that it wants me to define the value of x,y but what I want to do is to firstly Compare the value of a and b and then compare the value of c with the greater number I got by comparing a and b.
I'm pasting my code below:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("enter any three number::");
scanf("%d%d%d",&a,&b,&c);
if(x=(a>b)||(b>a));
printf("\n(a>b)||(b>a)=%d",&x);
{
if(y=(x>c)||(c>x));
printf("\n(y>c)||(c>y)=%d",&y);
}
getch();
return 0;
}
How can I improvise this? Hoping for positive response!
The problem is, you never defined x and y, as your compiler pointer out. Just define them like you did for other variables.
int a,b,c,x,y;
would do the job.
That said
you don't really need those if statements, as you used here.
The result of the relational operators (< / >) are not the numbers, it's an integer value, either 0 or 1.
For %d, you don't need to supply the address of the variable, just the variable is enough.
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 3 years ago.
Improve this question
I don't get what I should put in the place of: "α" and "β" in order to print all the 3 digit numbers which are different from 0 and from one another.
It is required to replace those 2 variables, with some code
We are entering G(0)!!!
It's from an exam paper, I really don't get it, please help.
void G(int k)
{int i;
for(i=1;i<=α;i++)
{ p[k]=i;
if(β)G(k+1);
else
printf("%d%d%d\n",p[0],p[1],p[2]);
}
}
For any of this to make sense, it must be that p is declared as a global int array of dimension at least 3. I assume for the purposes of this answer that it is in fact so declared.
Note that the function sets p[k] = i, but it later reads back only p[0], p[1], and p[2]. This should give you a pretty good idea about what makes sense for expression β, which controls whether to recurse (increasing k) or print.
Note also that the function sets p[k] = i, and that when it reads back those p[k] for various k, it wants to get values ranging from 1 to 9 (no more and no less). This should give you a pretty good idea of what expression makes sense for α, the inclusive upper bound on i.
Having figured those out, it remains to satisfy yourself that the natural substitutions for those expressions indeed produce a resulting function that behaves as required when initially called as G(0). I suspect that you will find that easier than you did discerning the needed expressions.
(Details are left as an exercise.)
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".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've got a question regarding an assignment i've been given.
-There are two integeras, a and b, which has the value of -1 and 1 respectively.
What's the value of a and b after running the following code and WHY.
if(!++a)
b+=a++;
When i run this code i'm getting the values 1 and 1. I can not really figure out WHY though... Im getting quite confused over the if statement, could anyone explain this for a beginner trying to learn C?
Your code is equivalent to this one:
int a = -1;
int b = 1;
a += 1;
if (a == 0) {
b += a;
a += 1;
}
You should see why both variables end up as 1 here. Now try to figure out why these codes are equivalent.
if (!++a)
First of all, if (a) is identical to if (a != 0). Weird, but true. Zero means false, any other number means true.
Saying if (!a) inverts that meaning.
++a increments a and returns the new value. (Unlike a++, which increments a but returns the old value, not the new one.)
Putting all that together, this says "increment a and test whether the answer is zero". If a = -1, then this is indeed true.
Usually people would write if (...) {do stuff}, but if the "do stuff" part is only one statement, you can leave out the brackets. We've already established that the condition is true in this case, so the "b+=a++" line is run.
If we put some spaces on that, we have
b += (a++);
So, increment a again, but before that, add it's (old) value to b.
++a gives you the value after increment, so you get 0 which means false. The ! operator makes it true.
Then you basically have
b = b + a;
a++;
So b remains 1 and a gets another increment and ends up at 1 too.
The main concept here is the difference between a++ and ++a. If you use a++ you will first get the value of a and then the value is incremented whereas for ++a the value is first incremented and then returned.
Initially a= -1 when your code will enter if (!++a) and it is pre increment first increment of a will happen so a will become 0 and !0 is 1 and it will enter in the if block.
now b+=a++;
here a++ is post increment so you can devide this statement in two parts.
first b+=a; b+=0 so b will remain 1.
second a++; a will become 1.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
What does this line of code mean?
if((c-a)!=0 && (d-b)!=0) goto trap;
What does the !=mean?
I inherited this, and I'm not sure what it means.
It means: if c is not equal to a, then if d and b are also not equal, goto (jump to) the line with the label "trap".
The !=, just means not equal. It is the complement of the == operator.
Not equal.
1 != 2
1 == 1
2 != 1
3 != 5
!= means not equal to.
ex: c-a is not equal to 0.
a != b means that a is not equal to b. It's the same as !(a == b).
Here's an english translation of the code...
if c minus a is not equal to 0 AND d minus b is not equal to 0 then goto trap