Why is a character constant more than size of charcter variable?

Discussion in 'C Programming' started by vb@gmail.com, Aug 5, 2005.

  1. Guest

    Hi all,
    I ran the follwing program on windows-XP(86 architecture) using gcc.

    #include <stdio.h>

    int main()
    {
    char ch='a';
    printf("%u %u\n",sizeof(ch),sizeof('a'));
    return 0;
    }
    The result was : 1 4

    My question is why a character constant gave size 4 while same
    character assigned to a char variable gave size 1.
    I am guessing that char constant was being treated like an integer-was
    it?
     
    , Aug 5, 2005
    #1
    1. Advertising

  2. Richard Bos Guest

    "" <> wrote:

    > #include <stdio.h>
    >
    > int main()
    > {
    > char ch='a';
    > printf("%u %u\n",sizeof(ch),sizeof('a'));
    > return 0;
    > }
    > The result was : 1 4
    >
    > My question is why a character constant gave size 4 while same
    > character assigned to a char variable gave size 1.
    > I am guessing that char constant was being treated like an integer-was
    > it?


    Yes. A character constant has type int. This doesn't seem logical, and
    IMO it isn't logical; but it is this way for hysterical reasons, which
    are related to those behind the default promotions for variadic
    functions.

    Richard
     
    Richard Bos, Aug 5, 2005
    #2
    1. Advertising

  3. wrote:
    > #include <stdio.h>
    >
    > int main()
    > {
    > char ch='a';
    > printf("%u %u\n",sizeof(ch),sizeof('a'));
    > return 0;
    > }
    > The result was : 1 4
    >
    > My question is why a character constant gave size 4 while same
    > character assigned to a char variable gave size 1.
    > I am guessing that char constant was being treated like an integer-was
    > it?


    If you change sizeof('a') into sizeof((char)'a') it will return 1. The
    fact that the first version returns 4 seems strange at first but it is
    quite logical. 'a' just translates into a number, and then sizeof has to
    determine the size. Compare it to sizeof(0x61), this will also return 4,
    even though I entered a value that can be contained in 1 byte, as can
    the 'a'.

    Kind regards,
    Johan

    --
    o o o o o o o . . . _____J_o_h_a_n___B_o_r_k_h_u_i_s___
    o _____ || http://www.borkhuis.com |
    .][__n_n_|DD[ ====_____ | |
    >(________|__|_[_________]_|________________________________|

    _/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
    == VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
     
    Johan Borkhuis, Aug 5, 2005
    #3
  4. On Fri, 05 Aug 2005 07:32:32 +0000, Richard Bos wrote:

    ....

    > Yes. A character constant has type int. This doesn't seem logical, and
    > IMO it isn't logical; but it is this way for hysterical reasons,


    I'm sure you're correct but there are historical reasons too.

    > which
    > are related to those behind the default promotions for variadic
    > functions.


    More specifically the integral promotions. In K&R C it was virtually (?)
    impossible to use a character value without it being promoted to int
    first, so making character constant int in the first place eliminated that
    step. There were and still are multi character constants such as 'abcd' or
    however many will fit in an int.

    Lawrence
     
    Lawrence Kirby, Aug 5, 2005
    #4
  5. Re: Why is a character constant more than size of charctervariable?

    "" <> writes:
    > I ran the follwing program on windows-XP(86 architecture) using gcc.
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char ch='a';
    > printf("%u %u\n",sizeof(ch),sizeof('a'));
    > return 0;
    > }
    > The result was : 1 4
    >
    > My question is why a character constant gave size 4 while same
    > character assigned to a char variable gave size 1.
    > I am guessing that char constant was being treated like an integer-was
    > it?


    No need to guess.

    <http://www.eskimo.com/~scs/C-faq/q8.9.html>

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Aug 6, 2005
    #5
  6. wrote on 05/08/05 :
    > char ch='a';
    > printf("%u %u\n",sizeof(ch),sizeof('a'));


    undefined behavior. sizeif returns a size_t.

    printf("%u %u\n",(unsigned) sizeof(ch),(unsigned) sizeof('a'));

    [C99]
    printf("%zu %zu\n",sizeof(ch),sizeof('a'));

    > return 0;
    > }
    > The result was : 1 4


    A character constant has the type of int. You draw the conclusion ...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Aug 6, 2005
    #6
  7. (supersedes <>)

    wrote on 05/08/05 :
    > char ch='a';
    > printf("%u %u\n",sizeof(ch),sizeof('a'));


    undefined behavior. sizeof returns a size_t.

    printf("%u %u\n",(unsigned) sizeof(ch),(unsigned) sizeof('a'));

    [C99]
    printf("%zu %zu\n",sizeof(ch),sizeof('a'));

    > return 0;
    > }
    > The result was : 1 4


    A character constant has the type of int. You draw the conclusion ...


    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
     
    Emmanuel Delahaye, Aug 6, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,220
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    8
    Views:
    574
    Peter Nilsson
    Mar 11, 2005
  3. bent lee

    Charcter Pointer Problem

    bent lee, Sep 22, 2006, in forum: C Programming
    Replies:
    12
    Views:
    464
    bent lee
    Sep 23, 2006
  4. Steven D'Aprano
    Replies:
    0
    Views:
    143
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    116
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page