By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,496 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,496 IT Pros & Developers. It's quick & easy.

sizeof a + b and strange value

P: n/a
#include<stdio.h>

int main(void)
{
unsigned int a=20,b=50, c = sizeof b+a;
printf("%d\n",c);
return 0;
}
out put:
24

Variable a is unchanged and + operator appends the variable b's value
(which is 4)
then throws the c value as 24.
When I change the as sizeof a+b the out put is 54. and so on.

My doubt is why is the second value (that is a) is unchanged and the
first shows the value
of sizeof(int). More correctly 4 + 20 = 24. Is there any operator
precedence involved?
If yes in what way. (I went through the precedence table but my mind
got stuck)

Posted via google so Thanks in advance
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Tue, 27 Jul 2004 03:33:50 -0700, dam_fool_2003 wrote:
#include<stdio.h>

int main(void)
{
unsigned int a=20,b=50, c = sizeof b+a;
printf("%d\n",c);
return 0;
}
out put:
24 Makes sense, sizeof(int) == 4 on your machine, and you add 20 to that.
Variable a is unchanged and + operator appends the variable b's value
(which is 4) b's value is not 4 as far as I can see. then throws the c value as 24.
When I change the as sizeof a+b the out put is 54. and so on. Yes, sizeof(int) == 4, and you add 50 to that. (and assign the
result to c)

My doubt is why is the second value (that is a) is unchanged and the
first shows the value sizeof applies only to one type at the time.
do you want something like sizeof a + sizeof b ?
of sizeof(int). More correctly 4 + 20 = 24.
Is there any operator precedence involved?

No. (well, sizeof a will be evaluated first)

Nov 14 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

da***********@yahoo.com wrote:
#include<stdio.h>

int main(void)
{
unsigned int a=20,b=50, c = sizeof b+a;
printf("%d\n",c);
return 0;
}
out put:
24

Variable a is unchanged and + operator appends the variable b's value
(which is 4)
then throws the c value as 24.
When I change the as sizeof a+b the out put is 54. and so on.
That's correct. And if you think about it a bit, you'll see why.
The sizeof operator has two forms

sizeof (something)

and
sizeof something

The 'something' in both forms is a single data item or data type

Now,
sizeof b+a
is evaluated as
(sizeof b) + a

If your compiler allocates four bytes to an int, then, in your case,
sizeof b will equal 4.

Tell me: 4 + a == what?

Well, if, as in your example,
unsigned int a=20;
then
4 + a == 24
so,
sizeof b + a
will evaluate to
(sizeof b) + a
or
4 + a
or
4 + 20
or
24
which is the result you are getting.

My doubt is why is the second value (that is a) is unchanged and the
first shows the value
of sizeof(int). More correctly 4 + 20 = 24. Is there any operator
precedence involved?
If yes in what way. (I went through the precedence table but my mind
got stuck)

Posted via google so Thanks in advance

- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBBkyWagVFX4UWr64RAhNPAJ9H28lF9g+VZEXheXjYxm iXtI+qgQCg5C9q
rUeD77PBcRIkr8rDCHggJNc=
=GsAC
-----END PGP SIGNATURE-----
Nov 14 '05 #3

P: n/a
Le mardi 27 juillet 2004 à 14:37, Lew Pitcher a écrit dans comp.lang.c*:
The sizeof operator has two forms

sizeof (something)

and
sizeof something

The 'something' in both forms is a single data item or data type


No. The sizeof operator has two forms:

sizeof type_cast

and

sizeof expression

..

A type_cast *must* have parentheses, an expression may or may not have
parentheses.

sizeof (int)
sizeof (unsigned long *)
sizeof 'A'
sizeof (var + 1)
etc.

--
___________ 2004-07-27 16:55:39
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
Nov 14 '05 #4

P: n/a
Serge Paccalin <sp@mailclub.no.spam.net.invalid> writes:
Le mardi 27 juillet 2004 à 14:37, Lew Pitcher a écrit dans comp.lang.c*:
The sizeof operator has two forms

sizeof (something)

and
sizeof something

The 'something' in both forms is a single data item or data type


No. The sizeof operator has two forms:

sizeof type_cast

and

sizeof expression


No, there is no "type_cast" in a sizeof expression. The parentheses
around the type in sizeof(type) have nothing to do with the
parentheses in a cast such as (type)expr.

This is a quibble about terminology; the rest of your conclusions are
correct.

The two forms are:

sizeof unary-expression
sizeof ( type-name )

Of course, the unary-expression can be a parenthesized expression.
The following are legal:

sizeof 42 /* 42 is a unary-expression */

sizeof(42) /* the parentheses are part of the operand, not
part of the syntax of the sizeof; this is legal
for the same reason that return(42); is legal */

sizeof(int) /* int is a type-name */

And of course you may need the parentheses to determine what the
operand is; "sizeof a + b" is not the same as "sizeof(a + b)".

But the following is illegal:

sizeof int /* BAD */

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

P: n/a
Something that calls itself da***********@yahoo.com wrote:

[snip]
cat main.c #include<stdio.h>

int main(int argc, char* argv[]) {
unsigned int a = 20, b = 50, c = sizeof(b + a);
printf("%d\n",c);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main

4
Nov 14 '05 #6

P: n/a
da***********@yahoo.com wrote in message news:<a3**************************@posting.google. com>...
#include<stdio.h>

int main(void)
{
unsigned int a=20,b=50, c = sizeof b+a;
printf("%d\n",c);
return 0;
}
out put:
24


"sizeof" is a compile-time operator... That means it is evaluated when
your C code is compiled. But the "+" operator (on non-constants) is a
run-time operator... That means it is evaluated when your program
actually runs. So...

Since the compiler cannot evaluate your "+" operator... It behaves
smart and takes only the first variable as the <operand> for the
"sizeof" operator. And replaces the "sizeof <operand>" instance with
it's value (which is 4 in your case). And...

The "+" operator operates on this value (4) and the remaining operand
at runtime. Em...

I think I'm clear...

- Gana
Nov 14 '05 #7

P: n/a
ga******@gmail.com (Ganesh) writes:
da***********@yahoo.com wrote in message
news:<a3**************************@posting.google. com>...
#include<stdio.h>

int main(void)
{
unsigned int a=20,b=50, c = sizeof b+a;
printf("%d\n",c);
return 0;
}
out put:
24


"sizeof" is a compile-time operator... That means it is evaluated when
your C code is compiled. But the "+" operator (on non-constants) is a
run-time operator... That means it is evaluated when your program
actually runs. So...

Since the compiler cannot evaluate your "+" operator... It behaves
smart and takes only the first variable as the <operand> for the
"sizeof" operator. And replaces the "sizeof <operand>" instance with
it's value (which is 4 in your case). And...

The "+" operator operates on this value (4) and the remaining operand
at runtime. Em...

I think I'm clear...


Yes, you're clear, but unfortunately you're wrong.

The distinction between compile-time and run-time operators has
absolutely nothing to do with the issue. It's just a matter of the
way the expression is parsed. Informally, the "sizeof" unary operator
has higher precedence than the "+" operator. The same thing applies
to the other unary operators, such as unary "-" and unary "*".

(I say "informally" because the standard doesn't really talk about
operator precedence; all this stuff is actually determined by the
grammar.)

For that matter, the references to b and a could be replaced with the
integer constants 50 and 20 (*), causing everything to be evaluted at
compilation time, and in C99 the sizeof operator applied to a VLA is
evaluated at run time. Again, this has no effect on how the
expressions are parsed.

The expression
sizeof b + a
is equivalent to
sizeof(b) + a

The spacing in
sizeof b+a
is misleading, but is ignored by the compiler, just as the spacing in
x * y+z
is ignored ("x*y + z" would be clearer).

If you want the sizeof to apply to the expression "b + a", you can do
so by adding parentheses:
sizeof(b+a)

(*) Replacing the reference to b and a with the integer constants 50
and 20 does change the type of the expression from unsigned int to
int, but that's not particularly significant.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.