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

Home Posts Topics Members FAQ

sizeof(x)

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;

size_t y= sizeof(x);
is not valid.
and only the following is valid:
int x;

size_t y= sizeof x;
However I am puzzled, and thought the first was also valid in
C90/C95(/C++03).

Mar 30 '08 #1
12 2747
On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
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;

size_t y= sizeof(x);
is not valid.
You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).
Mar 30 '08 #2
Harald van Dijk wrote:
On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
>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;

size_t y= sizeof(x);
is not valid.

You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).

I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
Mar 30 '08 #3
Ioannis Vranos wrote:
>
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"
Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
Mar 30 '08 #4
Ioannis Vranos wrote:
Ioannis Vranos wrote:
>I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.


More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

--
Ian Collins.
Mar 30 '08 #5
Ian Collins wrote:
Ioannis Vranos wrote:
>Ioannis Vranos wrote:
>>I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?
.... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.

Mar 30 '08 #6
On Sun, 30 Mar 2008 23:07:05 +0300, Ioannis Vranos wrote:
Harald van Dijk wrote:
>On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
>>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;

size_t y= sizeof(x);
is not valid.

You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).

I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.
That doesn't say sizeof(x) is invalid any more than the standard does.
Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
Yes, it does. Look at the grammar.

unary-expression:
sizeof unary-expression

unary-expression:
postfix-expression

postfix-expression:
primary-expression

primary-expression:
( expression )

A parenthesised expression is a primary-expression, which is a postfix-
expression, which is a unary-expression, which is a valid operand of
sizeof.

The standard doesn't explicitly mention that parenthesised expressions
are valid operands of +, -, *, /, ^, &, or pretty much any other
operator. The grammar makes that clear already.
Mar 30 '08 #7
On Sun, 30 Mar 2008 23:11:55 +0300, Ioannis Vranos wrote:
Ian Collins wrote:
>Ioannis Vranos wrote:
>>Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
sizeofx doesn't compile because there's no sizeof operator. There's a
sizeofx identifier. sizeof(x) does and should compile because sizeof( is
not a valid identifier. This is the same reason why a+++++b is not valid,
but a+++--b is: the first is split into {a}{++}{++}{+}{ b}, and the second
is split into {a}{++}{+}{--}{b}.
Mar 30 '08 #8
Ioannis Vranos wrote:
Ian Collins wrote:
>Ioannis Vranos wrote:
>>Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.
More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
Try sizeof((((((((( ((((((((((((x)) ))))))))))))))) ))))
and perhaps the answer will come to you.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Mar 30 '08 #9
Eric Sosman wrote:
Ioannis Vranos wrote:
>Ian Collins wrote:
>>Ioannis Vranos wrote:
Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.
More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"
Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.

Try sizeof((((((((( ((((((((((((x)) ))))))))))))))) ))))
and perhaps the answer will come to you.

Yes, it had already arrived. :-) Thanks anyway.

Mar 30 '08 #10

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

Similar topics

3
3556
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
2
2469
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"; }
19
9237
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
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
2416
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2539
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
90
8497
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
32
2593
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
5
2900
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof before runtime ? I also have: { void check_foo_size(void);
0
10484
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...
0
10251
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9072
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
7565
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...
0
5463
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.