473,473 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sizeof a + b and strange value

#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
7 1905
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
-----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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
37
by: thushianthan15 | last post by:
Greetings!! Currently i am learning C from K&R.I have DOS & Linux in my system.When i used sizeof() keyword to compute the size of integer , it shows different results in DOS and Linux.In DOS it...
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
15
by: subramanian100in | last post by:
Is it possible to measure the size of an array without using the sizeof operator ?
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.