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

sizeof 'a' returns 4

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
29 2656
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
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
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
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
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
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
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
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
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
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
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
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
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
"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
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
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
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
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
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
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
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
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
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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Charlie | last post by:
My Google Groups search on this one showed the endless debates and I did my best to look through them to see if my question was answered and didn't find it. So, here it goes.... From the GG...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
83
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
11
by: Ben C | last post by:
Why does this compile with no warnings, what, if anything, does it mean, and why does the program print out 1? #include <stdio.h> int main(void) { printf("%d\n", sizeof(int())); return 0; }
90
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
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
1
by: svlsr2000 | last post by:
Here is a small program which i saw a couple of years ago. #include<stdio.h> int main() { char x; if(sizeof(int)==sizeof('a')) printf("\n C compiler\n"); ...
15
by: somenath | last post by:
Hi All, I am not able to understand the behavior of the bellow mentioned program. #include<stdio.h> int main(void) { printf("\n Size = %d \n",sizeof 1<0); return 0;
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.