473,713 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof 'A'

Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Thanks,
Nishu
/*************** *************** ********/
#include<stdio. h>

int main(void)
{

const char ch = 'A';

printf("%d\n", sizeof ch);
printf("%d\n", sizeof 'A');

return 0;
}

/*************** ****/

Jul 10 '07 #1
58 7181
Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Because 'A' alone is an int.
Thanks,
Nishu
/*************** *************** ********/
#include<stdio. h>

int main(void)
{

const char ch = 'A';

printf("%d\n", sizeof ch);
printf("%d\n", sizeof 'A');

return 0;
}

/*************** ****/

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 10 '07 #2
On Jul 10, 1:07 am, Pietro Cerutti wrote:
Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?

Because 'A' alone is an int.
OK. What is the reason for considering it as int? I think we use
single quotes to say that it is a char.

Thanks,
Nishu
Jul 10 '07 #3
Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Thanks,
Nishu
/*************** *************** ********/
#include<stdio. h>

int main(void)
{

const char ch = 'A';

printf("%d\n", sizeof ch);
printf("%d\n", sizeof 'A');

return 0;
}

/*************** ****/
As Pietro said, 'A' is considered an int in C, not
a character. This is one of the many inconsistencies of the
language that you must learn by heart.

Why is this?

"Because that's the way it is..." :-)
Jul 10 '07 #4
On 10 Jul, 10:13, Nishu <naresh.at...@g mail.comwrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Because 'A' alone is an int.

OK. What is the reason for considering it as int?
Because that's what the standard says it is. The standard calls 'A' an
integer character constant...
I think we use
single quotes to say that it is a char.
No. We use single quotes to denote an integer character constant.
Jul 10 '07 #5
Nishu <na**********@g mail.comwrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:
Nishu wrote:
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Because 'A' alone is an int.
OK. What is the reason for considering it as int? I think we use
single quotes to say that it is a char.
For hysterical raisins, the type of a character constant is int. It's
one of the (IMO few) areas where C++ improves on C: in that language,
it's a char. But not, unfortunately, in C.

Richard
Jul 10 '07 #6
Nishu wrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:
Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Because 'A' alone is an int.
OK. What is the reason for considering it as int?
Actually it's considered an integer value, not necessarily an int. int
is just a particular integer type. So is short, long, long long etc.

The reason I suppose is a language design choice. By means of the
escape-sequence notation you can specify any arbitrary octal or
hexadecimal value, to encode character values that are not represented
in the source character set.
I think we use
single quotes to say that it is a char.
A sequence of one or more characters enclosed by single quotes is
regarded as a character constant, not a char. If it's a single
character, it's numerical value is the code for that character in the
machine's character set. Multi-character constants are treated in an
implementation defined manner.

Jul 10 '07 #7
On 10 Jul, 10:28, santosh <santosh....@gm ail.comwrote:
Nishu wrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:
Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Because 'A' alone is an int.
OK. What is the reason for considering it as int?

Actually it's considered an integer value, not necessarily an int. int
is just a particular integer type. So is short, long, long long etc.
The spec (I'm looking at C89) states that "An integer character
constant has type _int_"...

Jul 10 '07 #8
On Jul 10, 1:37 am, mark_blue...@po box.com wrote:
Because 'A' alone is an int.
OK. What is the reason for considering it as int?
Actually it's considered an integer value, not necessarily an int. int
is just a particular integer type. So is short, long, long long etc.

The spec (I'm looking at C89) states that "An integer character
constant has type _int_"...
OK. Thank you all.

-Nishu

Jul 10 '07 #9
mark_blue...@po box.com wrote:
On 10 Jul, 10:28, santosh <santosh....@gm ail.comwrote:
Nishu wrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:
Nishu wrote:
Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Because 'A' alone is an int.
OK. What is the reason for considering it as int?
Actually it's considered an integer value, not necessarily an int. int
is just a particular integer type. So is short, long, long long etc.

The spec (I'm looking at C89) states that "An integer character
constant has type _int_"...
You're right. Thanks for the correction and apologies to the OP.

Jul 10 '07 #10

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

Similar topics

3
3553
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
2
2461
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
9228
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
9
3019
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1930
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
2398
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2534
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
90
8421
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
2579
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
5
2895
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof before runtime ? I also have: { void check_foo_size(void);
0
8794
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9300
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9166
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9007
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4459
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.