0
c++ sizeof(type variabel)
sizeof(variabel) Will return the size of memory occupied or allocated. So from sololearn challenge: int s; int x= 10; s = sizeof(x=x+10), it should be equal to sizeof(x=20) or sizeof(20). Since 20 isan integer variabel, so c++ must allocate 4 bytes of memory space to occupied by int variabel x. Why sololearn Mark 20 as correct option?
8 Answers
+ 1
Oliver Pasaribu Can you post the exact challenge code?
It is 4 but not the way you think, sizeof doesn't evaluate expressions, but only the the size of the type that the expression yields, in compile time.
so it's not sizeof(20), do cout << x and you'll get 10. so
s = sizeof( (int) = (int) + (int) ) this yields an integer therefore it's 4.
+ 1
No, you don't even read my answer carefully Oliver Pasaribu !
First you write:
int s; int x= 10;
s = sizeof(x=x+10),
and apparently want the value of 's'
I told you it's 4 and also the reason why it is 4 and asked for "exact" code.
I also mentioned that sizeof operand expression doesn't get evaluated. then again you wrote:
int i = 10;
int s;
s = sizeof(x=x+20);
.......
something like that.
No! not "something like that" if that's the exact code it results in an error because 'x' is undefined.
Then again you say:
if int x = 10
then s=sizeof(x=x+20)
is identical to s=sizeof(30)
It's not! x has declared as int, that expression yields an int type, if int x=10, sizeof(x=x+10.5) is not sizeof(20.5). first is 4, second is 8.
It could be that:
int s, x=20;
s = sizeof(x = x + 10);
x = ?
in this case answer is 20.
I can't do any better unless you provide the "exact" question text, not "something like that"
0
The code is as follow:
int i = 10;
int s;
s = sizeof(x=x+20);
The output s=
a. 10
b. 2
c. 20
d. error'
e. 4.
Something like that.
0
I agree. Because cout<<sizeof(int) = 4. also sizeof(10) = 4. And if int x = 10, then s=sizeof(x=x+20) is identical with s=sizeof(30);
Is it right?
0
The assignment in () of sizeof does not compute, if cout x it will still be what it was originally assigned.
Whether it be 10, 20, or 30 sizeof will show 4 as it is int type. So s should == 4 with how I am seeing all this, unless looked wrong/misunderstood.
Which would mean sololearn may have an error
0
Bloody Halos yes, unfortunately many of these questions have wrong answers, copy pasted from web for some badged by noobs. I had a hard time with those.
On the hand it could be like the example in my previous post...
0
Yes. I am not really happy with this. It will increase my point but it lead me into big trouble in the future , when we enter the real world programming.
0
Indeed not very helpful. At least there are some in the community to help cover them but errors should be fixed.