why it prints 3?
-
Can anybody tell me why value of b is 3 in this code? what happens with (a=0) exactly?
int a=0;
int b=(a=0)?2:3;
cout<Because the statement within parenthesis, which assigns
0
toa
, equates to0
(false), thus assigning3
tob
. If you change it to:int b = (a = 1) ? 2 : 3;
it might make more sense.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Can anybody tell me why value of b is 3 in this code? what happens with (a=0) exactly?
int a=0;
int b=(a=0)?2:3;
cout<The result of the expression (assignment)
(a=0)
is
0
, i.e. the value ofa
(that isfalse
for the conditional operator?
). hence the result of the expressionVCD_A wrote:
(a=0)?2:3
is
3
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Because the statement within parenthesis, which assigns
0
toa
, equates to0
(false), thus assigning3
tob
. If you change it to:int b = (a = 1) ? 2 : 3;
it might make more sense.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
DavidCrow wrote:
change it to: int b = (a = 1) ? 2 : 3; it might make more sense.
Does it really make more sense? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
DavidCrow wrote:
change it to: int b = (a = 1) ? 2 : 3; it might make more sense.
Does it really make more sense? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]In that it would instead assign
2
tob
, yes."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
In that it would instead assign
2
tob
, yes."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Ah, OK, it makes a lot more sense... :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Can anybody tell me why value of b is 3 in this code? what happens with (a=0) exactly?
int a=0;
int b=(a=0)?2:3;
cout<a=0 is assignment. You probably wanted a==0 (two = signs), which is hte equality test. One of the things that's nice (when you're experienced), yet horrible (when you're learning) about C and C++ is that assignment is an expression, not a statement, which means that you can do what you've done there. In other languages like Ada, which class assignment as a statement, you couldn't make that mistake.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
a=0 is assignment. You probably wanted a==0 (two = signs), which is hte equality test. One of the things that's nice (when you're experienced), yet horrible (when you're learning) about C and C++ is that assignment is an expression, not a statement, which means that you can do what you've done there. In other languages like Ada, which class assignment as a statement, you couldn't make that mistake.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?
-
but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?
In C (and C++) 0 equates to false and non-zero equates to true. The case that you mentioned is similar to
int a = 0;
if (a)
{
b = 2;
}
else
{
b = 3;
}Since a is 0, this equates to if(0) or if(false). So the else part will be executed. But in the following snippet, the if part will be executed.
int a = -1;
if (a)
{
b = 2;
}
else
{
b = 3;
}«_Superman_» I love work. It gives me something to do between weekends.
-
but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?
-
but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?
VCD_A wrote:
but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true...
So what would you propose the value of
a
andb
to be in the following:int a, b, c;
a = b = c = 5;Hint: it's not
1
."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons