473,799 Members | 3,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof of expression & sizeof of type

Why does one need to use two kinds of sizeof operator:
* sizeof unary-expression,
* sizeof (type-name)
?

Their behavior seem not to be different (see an example below).

------ C++ code ------
#include <iostream>
using namespace std;

int main()
{
int x = 100;

cout << sizeof ++x << endl; // expression sizeof
cout << x << endl;

x = 100;
cout << endl;
cout << sizeof (++x) << endl; // type sizeof
cout << x << endl;

return 0;
}
----------------------

------ Run ------

4
100

4
100

-----------------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jun 10 '06
15 2248
Alex Vinokur wrote:

Thomas J. Gritzan wrote:

Which one do you think is redundant?


1) sizeof(int)
2) sizeof(x)
3) sizeof x // redundant


You are confused. 2 and 3 are the same thing, there just are
superfluous parentheses in 2. It's somewhat similar, although not
exactly, to this:
return (2);

return 2;

Those aren't redundant forms of return, just unneeded parentheses.


Brian
Jun 11 '06 #11
Default User <de***********@ yahoo.com> wrote:
Alex Vinokur wrote:
Thomas J. Gritzan wrote:
> Which one do you think is redundant?
1) sizeof(int)
2) sizeof(x)
3) sizeof x // redundant

You are confused. 2 and 3 are the same thing, there just are
superfluous parentheses in 2. It's somewhat similar, although not
exactly, to this: return (2); return 2;


Not exactly indeed, due to operator precedence.

int x;
long long y;
int z = sizeof x, y;

Gives you 4, while

int x=1;
int y=2;
int z = return 1, 2;

Gives you 2.

Steve
Jun 12 '06 #12
Steve Pope wrote:
Default User <de***********@ yahoo.com> wrote:
Alex Vinokur wrote:

Thomas J. Gritzan wrote: Which one do you think is redundant? 1) sizeof(int)
2) sizeof(x)
3) sizeof x // redundant

You are confused. 2 and 3 are the same thing, there just are
superfluous parentheses in 2. It's somewhat similar, although not
exactly, to this:

return (2);

return 2;


Not exactly indeed, due to operator precedence.

int x;
long long y;
int z = sizeof x, y;

Gives you 4, while

int x=1;
int y=2;
int z = return 1, 2;

Gives you 2.


What does any of that have to do with parentheses? The OP didn't
mention the comma operator, nor did I.

Brian
Jun 12 '06 #13
Default User <de***********@ yahoo.com> wrote:
Steve Pope wrote:
Default User <de***********@ yahoo.com> wrote:
> Alex Vinokur wrote:

>> Thomas J. Gritzan wrote:

>> > Which one do you think is redundant?

>> 1) sizeof(int)
>> 2) sizeof(x)
>> 3) sizeof x // redundant

> You are confused. 2 and 3 are the same thing, there just are
> superfluous parentheses in 2. It's somewhat similar, although not
> exactly, to this:

> return (2);

> return 2;
Not exactly indeed, due to operator precedence.

int x;
long long y;
int z = sizeof x, y;

Gives you 4, while

int x=1;
int y=2;
int z = return 1, 2;

Gives you 2.

What does any of that have to do with parentheses? The OP didn't
mention the comma operator, nor did I.


The "sizeof" operator has a certain precedence. (Look at
the table of operator precedences e.g. in Stroustrup.) The "return"
statement is not an operator, therefore has lower precedence
than any operator, therefore the entire expression to the right
of it is evaluated first.

That's how I look at it, in any case.

Steve
Jun 12 '06 #14
Alex Vinokur wrote:
Thomas J. Gritzan wrote:
[snip]

int x;

1) sizeof(int)
2) sizeof(x) (or sizeof x)

Which one do you think is redundant?


1) sizeof(int)
2) sizeof(x)
3) sizeof x // redundant


you forgot
4) sizeof ((x))
5) sizeof (((x)))
....

Jun 12 '06 #15
Steve Pope wrote:
Default User <de***********@ yahoo.com> wrote:
What does any of that have to do with parentheses? The OP didn't
mention the comma operator, nor did I.


The "sizeof" operator has a certain precedence.

That's how I look at it, in any case.


Hence the qualifier, "It's somewhat similar, although not exactly, to
this". Yeah, we can construct some examples where the () could matter,
but they don't for the ones the OP presented.

Brian

Jun 12 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
8916
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
2
2470
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
9
3025
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1939
by: dam_fool_2003 | last post by:
#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
42
2417
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
90
8498
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
28
6759
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
11
1596
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
12
2748
by: Ioannis Vranos | last post by:
In C99 it is mentioned: "The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.". If I am not wrong, this implies that int x;
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10237
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.