Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!usc!ucsd!ucbvax!hplabs!hpl-opus!hpnmdla!danm From: danm@hpnmdla.HP.COM (Dan Merget) Newsgroups: comp.lang.c Subject: Re: sizeof() confusion Message-ID: <4040002@hpnmdla.HP.COM> Date: 16 Nov 90 03:59:50 GMT References: <9156@latcs1.oz.au> Organization: HP Network Measurements Div, Santa Rosa, CA Lines: 39 In comp.lang.c, dhesi%cirrusl@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@hpnmdla.HP.COM