sizeof 'a' returns 4

L

lnitsu

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
 
R

Richard Heathfield

(e-mail address removed) 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.
 
F

Frederick Gotham

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;
}
 
L

lovecreatesbeauty

Richard said:
(e-mail address removed) said:
'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;
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
 
R

Richard Heathfield

lovecreatesbeauty said:
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.
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/.
 
F

Frederick Gotham

Richard Heathfield posted:
Frederick Gotham said:


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;
 
K

Kenny McCormack

Frederick Gotham said:
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
 
F

Frederick Gotham

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?
 
K

Kenny McCormack

Kenny McCormack posted:





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.
 
K

Keith Thompson

Frederick Gotham said:
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.
 
K

Keith Thompson

lovecreatesbeauty said:
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.
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.
 
S

Simon Lewis

Frederick Gotham said:
Richard Heathfield posted:



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.
 
K

Keith Thompson

Frederick Gotham said:
posted:

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).
 
D

Default User

Frederick said:
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
 
K

Kenny McCormack

Frederick said:
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.
 
K

Kenny McCormack

Simon Lewis said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top