By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

sizeof 'a' returns 4

P: n/a
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

Jun 18 '06 #1
Share this Question
Share on Google+
29 Replies


P: n/a
ln****@yahoo.com wrote:
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 ...


check out the following page, it's a FAQ problem: -)

http://www.c-faq.com/charstring/sizeofchar.html

Xicheng

Jun 18 '06 #2

P: n/a
Excellent, thank you very much.
Xicheng Jia wrote:
ln****@yahoo.com wrote:
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 ...


check out the following page, it's a FAQ problem: -)

http://www.c-faq.com/charstring/sizeofchar.html

Xicheng


Jun 18 '06 #3

P: n/a
Check out in this FAQ page:
http://www.c-faq.com/charstring/sizeofchar.html
ln****@yahoo.com wrote:
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


Jun 18 '06 #4

P: n/a
ln****@yahoo.com said:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?
(Note the argument is without parenthesis)


Good. No point splashing them everywhere for no reason.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #5

P: n/a
posted:
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

typedef T double;
unsigned SizeOf( T const t )
{
const char * const current = (const char*)&t;

const char * const next = (const char*)(&t + 1);

return next - current;
}


--

Frederick Gotham
Jun 18 '06 #6

P: n/a
Frederick Gotham said:
posted:
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

typedef T double;


Syntax error.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #7

P: n/a
Richard Heathfield wrote:
ln****@yahoo.com said:
why sizeof 'a' return 4?

'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?


But you seemed a little hesitant? If it's implementation-dependent, the
following equations are always true. It is true after my test :)

char c = 'c';
sizeof c == 1; /*by definition :)*/
sizeof 'c' == 4;
(Note the argument is without parenthesis)

Good. No point splashing them everywhere for no reason.


A pair of parentheses is required when type names applied to sizeof
operator. Is it because parentheses occur around type names in type
conventions?

lovecreatesbeauty

Jun 18 '06 #8

P: n/a
lovecreatesbeauty said:
Richard Heathfield wrote:
ln****@yahoo.com said:
> why sizeof 'a' return 4? 'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?


But you seemed a little hesitant?


Well, it might not be 4. It might simply be that you had a bug in your
program. Or maybe you /do/ have a bug in your program, but ints are still
four bytes wide on your system. Who knows? Insufficient information
available.
If it's implementation-dependent, the
following equations are always true.


....when using the version of your compiler that you are currently using,
with the same settings, on the same operating system on the same hardware,
yes.
> (Note the argument is without parenthesis)

Good. No point splashing them everywhere for no reason.


A pair of parentheses is required when type names applied to sizeof
operator.


I so rarely need to know that, because I almost never use sizeof to find the
size of a type. I'm far more interested in the size of an /object/.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #9

P: n/a
Richard Heathfield posted:
Frederick Gotham said:
posted:
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

typedef T double;


Syntax error.

So as not to clutter the newsgroup, I would request in future that, if
you choose to scrutanise and correct my code, that you correct the error
rather than simply indicate that an error is present -- it will be of
benefit to us all.
typedef double T;
Another thing, and this is your own perogative of course, maybe you'd
like to be more "social" about how you offer your counsel? It's pretty
clear that my error was more a question of "getting things muddled up"
rather than having a deficit of proficiency. When I myself correct such
silly errors, I am usually more sociable about it -- even one little
comment:

You've got them the wrong way around, should be:
typedef double T;


--

Frederick Gotham
Jun 18 '06 #10

P: n/a
In article <Xn***************************@194.125.133.14>,
Frederick Gotham <fg*******@SPAM.com> wrote:
....
Another thing, and this is your own perogative of course, maybe you'd
like to be more "social" about how you offer your counsel? It's pretty
clear that my error was more a question of "getting things muddled up"
rather than having a deficit of proficiency. When I myself correct such
silly errors, I am usually more sociable about it -- even one little
comment:

You've got them the wrong way around, should be:
typedef double T;


Since it is the custom around here to post URLs with pointed commentary
about how the OP should read them before asking any more dumb questions,
I offer up the following (which explains a lot about this ng):

http://en.wikipedia.org/wiki/Aspergers

Jun 18 '06 #11

P: n/a
Kenny McCormack posted:

Since it is the custom around here to post URLs with pointed commentary
about how the OP should read them before asking any more dumb questions,
I offer up the following (which explains a lot about this ng):

http://en.wikipedia.org/wiki/Aspergers


I'm not sure I understand what you're getting as... but are you suggesting
that this newsgroup bears some sort of association with Asperger Syndrome?

--

Frederick Gotham
Jun 18 '06 #12

P: n/a
In article <Xn***************************@194.125.133.14>,
Frederick Gotham <fg*******@SPAM.com> wrote:
Kenny McCormack posted:

Since it is the custom around here to post URLs with pointed commentary
about how the OP should read them before asking any more dumb questions,
I offer up the following (which explains a lot about this ng):

http://en.wikipedia.org/wiki/Aspergers


I'm not sure I understand what you're getting as... but are you suggesting
that this newsgroup bears some sort of association with Asperger Syndrome?


It has been theorized by many observers of this ng that many of the
so-called "regulars" suffer from some form(s) of the condition, and that
the ng serves as some kind of "support group" for them. It explains a
lot of the quirky responses you see here, and it is helpful to be
familiar with the condition when evaluating posts seen here.

Discussion of the condition itself is, of course, OT here, since there
is no mention of it in any of the C standards documents.

Jun 18 '06 #13

P: n/a
Frederick Gotham <fg*******@SPAM.com> writes:
Kenny McCormack posted: [snip] I'm not sure I understand what you're getting as... but are you suggesting
that this newsgroup bears some sort of association with Asperger Syndrome?


Kenny McCormack is a notorious self-described troll, who unfortunately
seems to have returned after a long and welcome absence. Please
ignore him.

If you don't want to take my word for this (and there's no real reason
why you should), feel free to search Google's archives for his past
"contributions" here.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #14

P: n/a
"lovecreatesbeauty" <lo***************@gmail.com> writes:
Richard Heathfield wrote:
ln****@yahoo.com said:
> why sizeof 'a' return 4?

'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?


But you seemed a little hesitant? If it's implementation-dependent, the
following equations are always true. It is true after my test :)

char c = 'c';
sizeof c == 1; /*by definition :)*/
sizeof 'c' == 4;


sizeof 'c' == sizeof(int)

sizeof(int) may or may not be 4.
> (Note the argument is without parenthesis)

Good. No point splashing them everywhere for no reason.


A pair of parentheses is required when type names applied to sizeof
operator. Is it because parentheses occur around type names in type
conventions?


I assume you mean "type conversions" (i.e., cast operators).

The syntax of the sizeof operator is

sizeof unary-expression
sizeof ( type-name )

Conceivably this could have been defined as:

sizeof unary-expression
sizeof type-name

but I think that would have led to ambiguities in some cases.

The parentheses around a type-name in a cast operator and in a sizeof
expression aren't necessarily related to each other. They're defined
separately in distinct parts of the grammar. They were probably added
for similar reasons in both cases (to avoid ambiguity), and for all I
know one might have inspired the other, but as far as the language
definition is concerned they're just two out of many uses of the "("
and ")" symbols.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #15

P: n/a
Frederick Gotham <fg*******@SPAM.com> writes:
Richard Heathfield posted:
Frederick Gotham said:
posted:

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
typedef T double;


Syntax error.

So as not to clutter the newsgroup, I would request in future that, if
you choose to scrutanise and correct my code, that you correct the error
rather than simply indicate that an error is present -- it will be of
benefit to us all.
typedef double T;
Another thing, and this is your own perogative of course, maybe you'd
like to be more "social" about how you offer your counsel? It's pretty


Some chance.

Just google up some examples of his "help". Certainly seems to think
he's something special : but all I can see are self grandizing smart ass
comments generally designed to bemuse the newbie. Aspergers is a great
description IMO.
Jun 18 '06 #16

P: n/a
Frederick Gotham <fg*******@SPAM.com> writes:
posted:
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


typedef T double;
unsigned SizeOf( T const t )
{
const char * const current = (const char*)&t;

const char * const next = (const char*)(&t + 1);

return next - current;
}


Was that supposed to be posted to a different thread? It doesn't seem
to have any relationship to the original question (which is question
8.9 of the comp.lang.c FAQ).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #17

P: n/a
Frederick Gotham wrote:
Kenny McCormack posted:
[blither blather]
I'm not sure I understand what you're getting as... but are you
suggesting that this newsgroup bears some sort of association with
Asperger Syndrome?

Kenny is a troll, whose intention is disrupt the newsgroup. Just ignore
him. If you use a killfile, he's a good candidate.

Brian
Jun 18 '06 #18

P: n/a
In article <4f*************@individual.net>,
Default User <de***********@yahoo.com> wrote:
Frederick Gotham wrote:
Kenny McCormack posted:


[blither blather]
I'm not sure I understand what you're getting as... but are you
suggesting that this newsgroup bears some sort of association with
Asperger Syndrome?

Kenny is a troll, whose intention is disrupt the newsgroup. Just ignore
him. If you use a killfile, he's a good candidate.


Man, you guys must really be worried.

Your responses are sooooo predictable.

Jun 18 '06 #19

P: n/a
In article <87************@gmail.com>,
Simon Lewis <si************@gmail.com> wrote:
....
Some chance.

Just google up some examples of his "help". Certainly seems to think
he's something special : but all I can see are self grandizing smart ass
comments generally designed to bemuse the newbie. Aspergers is a great
description IMO.


So true. So true. Welcome to the Light Side.

Jun 18 '06 #20

P: n/a
ga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <4f*************@individual.net>,
Default User <de***********@yahoo.com> wrote:
Frederick Gotham wrote:
Kenny McCormack posted:


[blither blather]
I'm not sure I understand what you're getting as... but are you
suggesting that this newsgroup bears some sort of association with
Asperger Syndrome?

Kenny is a troll, whose intention is disrupt the newsgroup. Just ignore
him. If you use a killfile, he's a good candidate.


Man, you guys must really be worried.

Your responses are sooooo predictable.


Just a general observation: when the teacher repeats the rules he's
only predictable if the pupil learnt them but doesn't give a damn
about them. That makes a brat... a modern time \sout{rock'n'roll}
troll. Some people grow up, eventually.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jun 18 '06 #21

P: n/a
On Sun, 18 Jun 2006 13:37:03 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
posted:
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

typedef T double;
unsigned SizeOf( T const t )
{
const char * const current = (const char*)&t;

const char * const next = (const char*)(&t + 1);

return next - current;
}


Your typedef has a syntax error.

What does the size of a double have to do with the original question?

Did you mean
typedef int T;

If the OP realized that 'a' was of type int (and not char), he
probably wouldn't have asked the question in the first place.
Remove del for email
Jun 19 '06 #22

P: n/a
In article <87************@gmail.com> Simon Lewis <si************@gmail.com> writes:
Frederick Gotham <fg*******@SPAM.com> writes:
Richard Heathfield posted:
Frederick Gotham said:
posted: ....> why sizeof 'a' return 4? .... typedef T double;

Syntax error.
So as not to clutter the newsgroup, I would request in future that,

.... Some chance.

Just google up some examples of his "help".
Well, if somebody posts a follow-up with completely irrelevant information,
you can expect some comments. I will give full comments:
typedef T double;

Interchange 'T' and 'double'
unsigned SizeOf( T const t )
{
const char * const current = (const char*)&t;
const char * const next = (const char*)(&t + 1);
return next - current;
}


This does not answer the original question (why sizeof 'a' == 4). I think
it was intended as a response to a question in another thread about a user
replacement of sizeof. And it also fails as that. It returns
sizeof (double). So can Frederick Gotham explain the intent of his
article?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 19 '06 #23

P: n/a
Richard Heathfield wrote:
Frederick Gotham said:

typedef T double;


Syntax error.


Interesting (?) fact I encountered while verifying your answer:

typedef double;

is NOT a syntax error; nor is:

double;

Jun 19 '06 #24

P: n/a

Old Wolf wrote:

Interesting (?) fact I encountered while verifying your answer:

typedef double;

is NOT a syntax error; nor is:

double;


Does following answer the question?

lovecreatesbeauty

/*quoting begins*/
A.2.2 Declarations

(6.7) declaration-specifiers:
storage-class-specifier declaration-specifiers(opt)
type-specifier declaration-specifiers(opt)
<snip>

(6.7.1) storage-class-specifier:
typedef
<snip>

(6.7.2) type-specifier:
<snip>
double
<snip>
/*quoting ends*/

Jun 19 '06 #25

P: n/a
lovecreatesbeauty wrote:
Old Wolf wrote:

Interesting (?) fact I encountered while verifying your answer:

typedef double;

is NOT a syntax error; nor is:

double;


Does following answer the question?


What question?

Jun 19 '06 #26

P: n/a
Old Wolf wrote:
Richard Heathfield wrote:
Frederick Gotham said:

typedef T double;


Syntax error.


Interesting (?) fact I encountered while verifying your answer:

typedef double;

is NOT a syntax error; nor is:

double;


Right, it's a constraint violation, and not a syntax error, because a
type-specifier followed by a semicolon can be a valid declaration:

struct S { /* ... */ };

Another interesting fact (? here too):

struct S { /* ... */ } typedef;

is allowed as well, and does not define any variable.

Jun 19 '06 #27

P: n/a
Frederick Gotham said:
Richard Heathfield posted:
Frederick Gotham said:
posted:

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
typedef T double;
Syntax error.

So as not to clutter the newsgroup, I would request in future that, if
you choose to scrutanise and correct my code, that you correct the error
rather than simply indicate that an error is present -- it will be of
benefit to us all.


Sorry, Frederick. I thought I had given sufficient information, given the
limited number of ways in which it could be wrong.
Another thing, and this is your own perogative of course, maybe you'd
like to be more "social" about how you offer your counsel?
Your original reply was not only an attempt to spoonfeed a homework answer
(always a bad idea) but was also an incorrect answer to the wrong question,
and it would not even compile.

This is your own prerogative, of course, but maybe you'd like to focus on
getting your own house in order before you start telling other people what
to write?
It's pretty
clear that my error was more a question of "getting things muddled up"
rather than having a deficit of proficiency.


My comment that your code had a syntax error was not a claim that you had a
"deficit of proficiency". It was a comment that your code had a syntax
error. These are not the same thing. But you can try for "deficit of
proficiency" if you like.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 19 '06 #28

P: n/a
On Sun, 18 Jun 2006 22:23:20 +0200, in comp.lang.c , Simon Lewis
<si************@gmail.com> wrote:
Just google up some examples of his "help". Certainly seems to think
he's something special : but all I can see are self grandizing smart ass
comments generally designed to bemuse the newbie. Aspergers is a great
description IMO.


Congrats, you get the fsckwit of the week award.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 19 '06 #29

P: n/a
On Sun, 18 Jun 2006 22:41:39 +0000 (UTC), in comp.lang.c ,
ga*****@xmission.xmission.com (Kenny McCormack) wrote:
In article <4f*************@individual.net>,
Default User <de***********@yahoo.com> wrote:
Frederick Gotham wrote:
Kenny McCormack posted:


[blither blather]
I'm not sure I understand what you're getting as... but are you
suggesting that this newsgroup bears some sort of association with
Asperger Syndrome?

Kenny is a troll, whose intention is disrupt the newsgroup. Just ignore
him. If you use a killfile, he's a good candidate.


Man, you guys must really be worried.

Your responses are sooooo predictable.


*replonk*
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 19 '06 #30

This discussion thread is closed

Replies have been disabled for this discussion.