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

V

vb

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?
 
R

Richard Bos

#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
 
J

Johan Borkhuis

#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 address removed) |
>(________|__|_[_________]_|________________________________|
_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
 
L

Lawrence Kirby

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
 
K

Keith Thompson

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>
 
E

Emmanuel Delahaye

E

Emmanuel Delahaye

(supersedes <[email protected]>)

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?"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top