473,809 Members | 2,735 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
12 2749
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
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.
C code is split into tokens before those tokens are parsed. (The
process actually involves "preprocess or tokens", which are later
converted to "tokens", but that doesn't matter in this case.)

``sizeof'' is a keyword; like all keywords, it has the form of an
identifier. ``('' and ``)'' are punctuators. Two adjacent
identifiers, keywords, or numeric constants must be separated by
whitespace (a comment counts as whitespace). A keyword and a
punctuator don't need any whitespace to separate them.

``sizeofx'' is just a single identifier that has nothing to do with
the ``sizeof'' keyword. It compiles just fine if you happen to have
declared it:

int sizeofx = 42;
sizeofx;

``sizeof x'' is two tokens, ``sizeof'' and ``x''. If ``x'' is an
expression (actually a unary-expression; see the grammar), then that's
a legal expression.

``sizeof(x)'' is four tokens, ``sizeof'', ``('', ``x'', and ``)''. If
``x'' is a type-name, then that's a valid expression. If ``x'' is an
expression, then ``(x)'' is also a valid expression, and the whole
thing is also a valid expression.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 30 '08 #11
On Mar 30, 1:11*pm, Ioannis Vranos <ivra...@nospam .no.spamfreemai l.gr>
wrote:
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
Good grief!

Do you realize that children that were in junior highschool back when
you first started being a goof here now have CS degrees and software
jobs?

You need to disable your anti-learning filter, or at least reduce the
coefficients in its confusion matrix a little bit.
Mar 30 '08 #12
In article <fs***********@ ulysses.noc.ntu a.gr>,
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrote:
>Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
A parenthesized expression is an expression, (that's a fact about C
grammar, not the English language). sizeof(x) is no more a problem
than a[(x)].

-- Richard
--
:wq
Mar 31 '08 #13

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

Similar topics

3
3558
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
2471
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
9238
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
3026
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
1942
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
2419
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2540
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
8502
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
2596
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
2901
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
9721
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
10639
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
10376
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...
1
10383
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
9200
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
7661
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
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4332
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
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.