473,326 Members | 2,133 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,326 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 6163

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.