Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why is a character constant more than size of charcter variable?

Reply
Thread Tools

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

 
 
vb@gmail.com
Guest
Posts: n/a
 
      08-05-2005
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?

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      08-05-2005
"(E-Mail Removed)" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Johan Borkhuis
Guest
Posts: n/a
 
      08-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[ ====_____ | (E-Mail Removed) |
>(________|__|_[_________]_|________________________________|

_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      08-05-2005
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-06-2005
"(E-Mail Removed)" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-06-2005
(E-Mail Removed) 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"


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-06-2005
(supersedes <(E-Mail Removed)>)

(E-Mail Removed) 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?"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Charcter Pointer Problem bent lee C Programming 12 09-23-2006 04:15 AM
Getting the xth bit of an unsigned charcter John.Doe.UK@gmail.com C Programming 8 03-11-2005 09:02 PM
End Record Charcter Bruce.Dobson Perl Misc 2 10-11-2003 11:09 PM



Advertisments