473,288 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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 1897
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.