sizeof a + b and strange value

D

dam_fool_2003

#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
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

#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)
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

#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+VZEXheXjYxmiXtI+qgQCg5C9q
rUeD77PBcRIkr8rDCHggJNc=
=GsAC
-----END PGP SIGNATURE-----
 
S

Serge Paccalin

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
 
K

Keith Thompson

Serge Paccalin said:
Le mardi 27 juillet 2004 à 14:37, Lew Pitcher a écrit dans comp.lang.c :

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 */
 
E

E. Robert Tisdale

Something that calls itself (e-mail address removed) 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
 
G

Ganesh

#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
 
K

Keith Thompson

(e-mail address removed) wrote in message


"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top