sizeof() confusion

Dan Merget danm at hpnmdla.HP.COM
Fri Nov 16 14:59:50 AEST 1990


In comp.lang.c, dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes:

> >>        printf("sizeof c       = %d\n", sizeof c);
> >>        printf("sizeof 'c'     = %d\n", sizeof 'c');

> >Were you suprised that "'c'" was 4?
> >You shouldn't be.  That expression evaluates to an int, not a char.
                      ^^^^
> If characters are promoted to ints in expressions, then why isn't
> |sizeof c| equivalent to |sizeof (int) c|?  The confusion arises
> because the term "expression" is defined differently in the definition
> of C and in colloquoal conversation.
                                                                      ********
                                                    ****  ****  ****  *      *
no no no No No No NO NO NO NO NO NO *NO* *NO* *NO*  *NO*  *NO*  *NO*  * NO!! *
                           ^^ ^^ ^^  ^^   ^^   ^^   ****  ****  ****  *      *
                                                                      ********

The confusion has nothing to do with the definition of "expression", and
nothing is getting promoted.  The poster did not say "All chars are promoted
to ints in expressions"; he said "That expression ["'c'"] evaluates to an int".

Remember, a "character" in c is simply the integer which corresponds to the
ASCII (or EBCDIC, etc) representation of that character.  When storing an array
of characters, you should use the 8-bit "char" type.  However, when you are
dealing with an individual character, you will get better milage out of the
"int" type.  In recognition of this fact, A CHARACTER BETWEEN TWO SINGLE
QUOTES IS AN **INT**!  EVEN THOUGH THEY CALL IT A "CHARACTER CONSTANT", IT IS
**NOT** AN 8-BIT CHAR!  (Exception: some compiler optimizations)
       _
      | |   /\           ________________________________________________
    __| |__/ /_/\____/\__\   This note is not intended to represent the  \
   / _  |_  __/  \__/  \___  viewpoints of my god, my country, my company,\
  / / | |/ / / /\ \/ /\ \  \ or my family.  I'm not even certain that      \
  \ \_| |\ \/\ \ \  / / /  / it accurately represents my own.              /
   \____| \__/\/  \/  \/  /_______________________________________________/

    Dan Merget
    danm at hpnmdla.HP.COM



More information about the Comp.lang.c mailing list