473,769 Members | 8,305 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 7202
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
3556
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
2469
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
9237
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
3025
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
1939
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
2416
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2538
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
8476
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
2591
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
2900
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
9589
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
9423
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
10219
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...
1
7413
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
5310
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.