|
|
Up |
|
|
  |
Author: NishuNishu 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 |
|
  |
Author: Pietro CeruttiPietro 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 |
|
  |
Author: NishuNishu 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 |
|
  |
Author: jacob naviajacob 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 |
|
  |
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 |
|
  |
Author: Richard BosRichard 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 |
|
  |
Author: santoshsantosh 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 |
|
  |
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 |
|
  |
Author: NishuNishu 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 |
|
  |
|
|
  |
|
|
|
|
|