473,473 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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?

Nov 15 '05 #1
6 3630
"vb@gmail.com" <vb*****@gmail.com> 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
Nov 15 '05 #2
vb@gmail.com 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[ ====_____ | jo***@borkhuis.com |(________|__|_[_________]_|________________________________|

_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
Nov 15 '05 #3
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
Nov 15 '05 #4
"vb@gmail.com" <vb*****@gmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #5
vb@gmail.com 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"
Nov 15 '05 #6
(supersedes <mn***********************@YOURBRAnoos.fr>)

vb@gmail.com 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?"
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
"Garp" <garp7@no7.blueyonder.co.uk> wrote in message news:<_vpuc.1424$j_3.13346038@news-text.cableinet.net>... > "lawrence" <lkrubner@geocities.com> wrote in message >...
9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
15
by: Ramaraj M Bijur | last post by:
Hi All, Could anyone help me to resolve following problem in C the IDE used is Microsoft VC++, Please let me know the which option in VC++ will do the needful The problem statement:...
15
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added...
23
by: Akhil | last post by:
Since a character constant is an int value represented as a character in single quotes,so it is treated as a 1 byte integer now look at the following snippet. #include<stdio.h> int main(void)...
2
by: John | last post by:
I want to declare a somewhat larger string constant which doesn't fit on one line. I tried several ways without luck. Is it even possible to declare it on more than one line? Thanks, john
2
by: Mirco Wahab | last post by:
After reading through some (open) Intel (CPU detection) C++ source (www.intel.com/cd/ids/developer/asmo-na/eng/276611.htm) I stumbled upon a sketchy use of multibyte characters - - - - - - - - -...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.