473,395 Members | 1,986 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,395 software developers and data experts.

Using sizeof without parentheses

I've been trying to wean myself off using parentheses after the sizeof
operator (and after the return keyword, too), but my understanding is
challenged by the 4th use of sizeof in the following code:

#include <stdio.h>

struct {
int i;
char *p;
} var;

typedef struct {
int i;
char *p;
} TYPE;

int main(int argc, char *argv[])
{
printf("1: %d\n", sizeof var);
printf("2: %d\n", sizeof(var));

printf("3: %d\n", sizeof TYPE);
printf("4: %d\n", sizeof(TYPE));

return 0;
}

I understand that (TYPE) is not a value, so is the 3rd use of sizeof a
sort of 'special case' of what sizeof permits?
(gcc v4.0.1 and earlier, just to make this possibly OT)

A bit confused; Thanks,

--
Chris.
Apr 10 '06 #1
5 6170

Chris McDonald wrote:
I've been trying to wean myself off using parentheses after the sizeof
operator (and after the return keyword, too), but my understanding is
challenged by the 4th use of sizeof in the following code:

#include <stdio.h>

struct {
int i;
char *p;
} var;

typedef struct {
int i;
char *p;
} TYPE;

int main(int argc, char *argv[])
{
printf("1: %d\n", sizeof var);
This is fine.
printf("2: %d\n", sizeof(var));
No parenthesis required. Even if you opt for them, it's misleading to
"glue" them to `sizeof`.
printf("3: %d\n", sizeof TYPE);
This is not allowed, as `TYPE` is a type and parenthesis are required.

If your compiler didn't pick this up, it's either broken, or you need
to turn the warning/error settings to a higher level.
printf("4: %d\n", sizeof(TYPE));
This is as it should be.
return 0;
}

I understand that (TYPE) is not a value, so is the 3rd use of sizeof a
sort of 'special case' of what sizeof permits?
(gcc v4.0.1 and earlier, just to make this possibly OT)


See above. (GCC may allow it as an extension, but it is OT.)

Apr 10 '06 #2
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Chris McDonald wrote:
printf("3: %d\n", sizeof TYPE);
This is not allowed, as `TYPE` is a type and parenthesis are required. If your compiler didn't pick this up, it's either broken, or you need
to turn the warning/error settings to a higher level. printf("4: %d\n", sizeof(TYPE));

This is as it should be.

Thanks Vladimir; gcc seems to be doing fine reporting #3 as an error,
even without any additional flags.

But I'm a little confused as to why if "... `TYPE` is a type"
that by placing parenthesis around the type name, that it's now OK?

--
Chris.
Apr 10 '06 #3
Chris McDonald wrote:
I've been trying to wean myself off using parentheses after the sizeof
operator (and after the return keyword, too), but my understanding is
challenged by the 4th use of sizeof in the following code:

#include <stdio.h>

struct {
int i;
char *p;
} var;

typedef struct {
int i;
char *p;
} TYPE;

int main(int argc, char *argv[])
{
printf("1: %d\n", sizeof var);
printf("2: %d\n", sizeof(var));

printf("3: %d\n", sizeof TYPE);
printf("4: %d\n", sizeof(TYPE));

return 0;
}

I understand that (TYPE) is not a value, so is the 3rd use of sizeof a
sort of 'special case' of what sizeof permits?
(gcc v4.0.1 and earlier, just to make this possibly OT)

A bit confused; Thanks,


The third use is an error for which the compiler must
issue a diagnostic. It is not some kind of special case
for sizeof; the construct simply doesn't work at all. (If
it works for your compiler, you are probably running the
compiler in a "C-with-extras" mode; gcc is famous/notorious
for creative departures from Standard C. Try using the
command-line options "-W -Wall -ansi -pedantic" or similar.)

By the way, "%d" is an incorrect conversion specifier for
the result of sizeof. "%d" must match an ordinary signed int,
but sizeof produces a size_t. The precise makeup of size_t
varies from one compiler to another, but it is certainly not
a signed value. I have used compilers where all of the examples
you show (except #3, which doesn't compile) would print zero.

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 10 '06 #4

Chris McDonald wrote:
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Chris McDonald wrote:
printf("3: %d\n", sizeof TYPE);
This is not allowed, as `TYPE` is a type and parenthesis are required.

If your compiler didn't pick this up, it's either broken, or you need
to turn the warning/error settings to a higher level.

printf("4: %d\n", sizeof(TYPE));

This is as it should be.

Thanks Vladimir; gcc seems to be doing fine reporting #3 as an error,
even without any additional flags.

But I'm a little confused as to why if "... `TYPE` is a type"
that by placing parenthesis around the type name, that it's now OK?


C99
6.5.3.4.2
The sizeof operator yields the size (in bytes) of its operand,
which may be an expression or the parenthesized name
of a type.

Apr 10 '06 #5
"Vladimir S. Oka" <no****@btopenworld.com> writes:
C99
6.5.3.4.2
The sizeof operator yields the size (in bytes) of its operand,
which may be an expression or the parenthesized name
of a type.


Thanks;

--
Chris.
Apr 10 '06 #6

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;
32
by: Mike Machuidel | last post by:
Hi, I'm a game developer programming mostly in C and ASM for about 7 years. Today at work a colleague (a C++ programmer) yelled at me I'm a bad C programmer because I use "return(0);" instead...
7
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
3
by: srivatsan_b | last post by:
Can anybody please explain how sizeof operator is implemented? Thanks in advance Srivatsan B
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
29
by: lnitsu | last post by:
Hello I've got a simple question regarding sizeof operator. why sizeof 'a' return 4? (Note the argument is without parenthesis) If anybody is so kind to answer ... TIA
43
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
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...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.