473,549 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ 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 1918
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***********@y ahoo.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)

iD8DBQFBBkyWagV FX4UWr64RAhNPAJ 9H28lF9g+VZEXhe XjYxmiXtI+qgQCg 5C9q
rUeD77PBcRIkr8r DCHggJNc=
=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.inval id> 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_Keit h) 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***********@y ahoo.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***********@y ahoo.com wrote in message news:<a3******* *************** ****@posting.go ogle.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***********@y ahoo.com wrote in message
news:<a3******* *************** ****@posting.go ogle.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_Keit h) 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
4260
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
37
1768
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 shows integer occupies 2 bytes , but in Linux it shows 4 bytes.Is sizeof operator "really" shows the machine data or register size ? Please help...
12
2399
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
2690
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 work on all implementation. size_t size_obj = (char*)(&obj + 1) - (char*)(&obj); I wanted to find out on which implementation this would fail....
40
3592
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 int) ? Same question for the corresponding unsigned types.
15
6537
by: subramanian100in | last post by:
Is it possible to measure the size of an array without using the sizeof operator ?
17
4085
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
6341
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 "right" way to go about calling malloc instead is to dereference a pointer; apparently even if said pointer is undefined. E.g. ptr =...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.