sizeof 'A'
  Home FAQ Contact Sign in
comp.lang.c only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c Profile…
 Up
sizeof 'A'         


Author: Nishu
Date: Jul 10, 2007 02:01

Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Thanks,
Nishu
/**************************************/
#include

int main(void)
{

const char ch = 'A';

printf("%%d\n", sizeof ch);
printf("%%d\n", sizeof 'A');

return 0;
}

/*******************/
56 Comments
Re: sizeof 'A'         


Author: Pietro Cerutti
Date: Jul 10, 2007 02:07

Nishu wrote:
> Hi All,
> When I run the below program in MSVC, I get the output as
> 1
> 4
> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
> or compiler specific?

Because 'A' alone is an int.
Show full article (0.54Kb)
1 Comment
Re: sizeof 'A'         


Author: Nishu
Date: Jul 10, 2007 02:13

On Jul 10, 1:07 am, Pietro Cerutti wrote:
> Nishu wrote:
>> Hi All,
>> When I run the below program in MSVC, I get the output as
>> 1
>> 4
>> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
>> or compiler specific?
>
> Because 'A' alone is an int.
>
OK. What is the reason for considering it as int? I think we use
single quotes to say that it is a char.

Thanks,
Nishu
no comments
Re: sizeof 'A'         


Author: jacob navia
Date: Jul 10, 2007 02:17

Nishu wrote:
> Hi All,
> When I run the below program in MSVC, I get the output as
> 1
> 4
> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
> or compiler specific?
> Thanks,
> Nishu
> /**************************************/
> #include
>
> int main(void)
> {
>
> const char ch = 'A';
>
> printf("%%d\n", sizeof ch);
> printf("%%d\n", sizeof 'A');
> ...
Show full article (0.66Kb)
1 Comment
Re: sizeof 'A'         


Date: Jul 10, 2007 02:20

On 10 Jul, 10:13, Nishu gmail.com> wrote:
> On Jul 10, 1:07 am, Pietro Cerutti wrote:> Nishu wrote:
>>> Hi All,
>>> When I run the below program in MSVC, I get the output as
>>> 1
>>> 4
>>> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
>>> or compiler specific?
>
>> Because 'A' alone is an int.
>
> OK. What is the reason for considering it as int?

Because that's what the standard says it is. The standard calls 'A' an
integer character constant...
> I think we use
> single quotes to say that it is a char.

No. We use single quotes to denote an integer character constant.
no comments
Re: sizeof 'A'         


Author: Richard Bos
Date: Jul 10, 2007 02:23

Nishu gmail.com> wrote:
> On Jul 10, 1:07 am, Pietro Cerutti wrote:
>> Nishu wrote:
>>> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
>>> or compiler specific?
>>
>> Because 'A' alone is an int.
>>
> OK. What is the reason for considering it as int? I think we use
> single quotes to say that it is a char.

For hysterical raisins, the type of a character constant is int. It's
one of the (IMO few) areas where C++ improves on C: in that language,
it's a char. But not, unfortunately, in C.

Richard
no comments
Re: sizeof 'A'         


Author: santosh
Date: Jul 10, 2007 02:28

Nishu wrote:
> On Jul 10, 1:07 am, Pietro Cerutti wrote:
>> Nishu wrote:
>>> Hi All,
>>> When I run the below program in MSVC, I get the output as
>>> 1
>>> 4
>>> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
>>> or compiler specific?
>>
>> Because 'A' alone is an int.
>>
> OK. What is the reason for considering it as int?

Actually it's considered an integer value, not necessarily an int. int
is just a particular integer type. So is short, long, long long etc.

The reason I suppose is a language design choice. By means of the
escape-sequence notation you can specify any arbitrary octal or
hexadecimal value, to encode character values that are not represented
in the source character set.
Show full article (1.10Kb)
no comments
Re: sizeof 'A'         


Date: Jul 10, 2007 02:37

On 10 Jul, 10:28, santosh gmail.com> wrote:
> Nishu wrote:
>> On Jul 10, 1:07 am, Pietro Cerutti wrote:
>>> Nishu wrote:
>>>> Hi All,
>>>> When I run the below program in MSVC, I get the output as
>>>> 1
>>>> 4
>>>> Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
>>>> or compiler specific?
>
>>> Because 'A' alone is an int.
>
>> OK. What is the reason for considering it as int?
>
> Actually it's considered an integer value, not necessarily an int. int
> is just a particular integer type. So is short, long, long long etc.

The spec (I'm looking at C89) states that "An integer character
constant has type _int_"...
no comments
Re: sizeof 'A'         


Author: Nishu
Date: Jul 10, 2007 02:48

On Jul 10, 1:37 am, mark_blue...@pobox.com wrote:
>>>> Because 'A' alone is an int.
>
>>> OK. What is the reason for considering it as int?
>
>> Actually it's considered an integer value, not necessarily an int. int
>> is just a particular integer type. So is short, long, long long etc.
>
> The spec (I'm looking at C89) states that "An integer character
> constant has type _int_"...

OK. Thank you all.

-Nishu
no comments
Re: sizeof 'A'         


Author: santosh
Date: Jul 10, 2007 03:02

mark_blue...@pobox.com wrote:
Show full article (0.82Kb)
no comments
1 2 3 4 5 6