Re: Any solution ??

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 11/06/04


Date: Sat, 06 Nov 2004 02:17:18 GMT


"Watson Davis" <watson@watsonmusic.com> wrote in message
news:Xns959884037C9A9thepencilneckyahooco@206.66.12.203...
> James Dennett <jdennett@acm.org> wrote in
> news:kpNid.107257$hj.86016@fed1read07:
>
> > chahnaz.ouzikene wrote:
> >
> >>
> >> How come ? are chars stored in 2 bytes in C++ ????
> >
> > No; the C++ version of the program will always return 1,
> > but the C version will return sizeof(int), which is likely
> > to be 2, 4, or 8 (though it could possibly be 1 on some
> > unusual platforms).
> >
> >> or is sizeof working differently ??
> >
> > No; char works differently. In C, 'a' is of type int,
> > but in C++ it is of type char. In C, there aren't many
> > ways to determine its type; sizeof is one way which can
> > be affected by sizeof(char).
>
> So when C sees a literal character, it takes its size as though it were an
> int?

No, not "as though", In C, the type of a character literal *is* 'int'.

> This is interesting. Of course, I couldn't trust you guys... :)
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char character;
>
> printf("Size of literal: %d\n\n", sizeof('a'));
> printf("Size of char: %d\n\n", sizeof(character));
> return 0;
> }
>
> OUTPUT:
>
> Size of literal: 4
>
> Size of char: 1
>
>
> I did not know it would do that. I ran it on 3 different systems
(Windows,
> Linus, and Solaris) to see if there would be any change in the literal
> size, there wasn't.

There could have been. It depends upon sizeof(int), which can
and does vary among platforms.

>I also did it in C++ and it came up as 1 both ways.

Yes, because in C++, the type of a character literal is 'char'.
This is one of those 'subtle' differences between the two
languages.

In both languages, the size of a type 'char' object
is always 1.

> I like this group. You guys are OK. :)
>
> Let me apologize beforehand about shooting my mouth off about things I
> don't understand. I'll try to be better in the future.

Good books help with that. :-)

-Mike



Relevant Pages

  • Re: doubly-linked list & sorting
    ... char Emess; ... diff = -1; ...
    (comp.lang.c)
  • Re: Mathematics of the Enigma cipher?
    ... A character is mapped to an integer which becomes ... All the Rotors should be advanced ... char val3=R2.GetCharacterIndex; ... I thought the Reflector was fixed. ...
    (comp.programming)
  • Re: Mathematics of the Enigma cipher?
    ... A character is mapped to an integer which becomes ... All the Rotors should be advanced ... char val3=R2.GetCharacterIndex; ... I thought the Reflector was fixed. ...
    (comp.programming)
  • Re: Pointer
    ... require in input a pointer char: ... unsafe public static extern int OpenFile; ... What is it pointing to exactly, is it pointing to a "Unicode character" array or is it pointing to a "Single byte character" array or is it pointing to something else? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pointer
    ... require in input a pointer char: ... unsafe public static extern int OpenFile; ... What is it pointing to exactly, is it pointing to a "Unicode character" array or is it ...
    (microsoft.public.dotnet.languages.csharp)