473,915 Members | 3,885 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
58 7229
jacob navia wrote:
>
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.
Just curious: With what is the int-ness of 'A' "inconsiste nt?"

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 10 '07 #11
Eric Sosman wrote:
jacob navia wrote:
>>
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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"
With it being a character of course.
Jul 10 '07 #12
jacob navia wrote:
Eric Sosman wrote:
>jacob navia wrote:
>>>
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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"

With it being a character of course.
It's consistent with getchar/putchar - I always thought that was the reason.
Jul 10 '07 #13
Richard Harnden wrote:
jacob navia wrote:
Eric Sosman wrote:
jacob navia wrote:

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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"
With it being a character of course.

It's consistent with getchar/putchar - I always thought that was the reason.
They were probably designed after the core language features were
decided upon.
getchar/putchar could very well return an int while character
constants could be of type char.

Jul 10 '07 #14
Richard Harnden <ri*****@oninit .comwrites:
jacob navia wrote:
Eric Sosman wrote:
jacob navia wrote:

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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"
With it being a character of course.

It's consistent with getchar/putchar - I always thought that was the reason.
Is it? putchar('\xFE') doesn't do the right thing if char is signed.

Yours,

--
Jean-Marc
Jul 10 '07 #15
jacob navia wrote On 07/10/07 08:40,:
Eric Sosman wrote:
>>jacob navia wrote:
>>>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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"


With it being a character of course.
... but since it *isn't* a character, you might just
as well say its int-ness is inconsistent with it being
a struct tm. The int-ness of 'A' may be surprising[*],
but I can't think of any convention or practice elsewhere
in the language that establishes a pattern with which
it could be inconsistent.
[*] Or maybe not, when you consider the usual
arithmetic conversions (6.3.1.8). With the exception
of sizeof, every operator[**] you could apply to a
char-type 'A' would promote its value to int or unsigned
int anyhow.

[**] If I've overlooked something, I'm confident
someone will remind me.

--
Er*********@sun .com
Jul 10 '07 #16
On Tue, 10 Jul 2007 14:40:52 +0200, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>Eric Sosman wrote:
>jacob navia wrote:
>>>
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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"

With it being a character of course.
Where does it say that characters are the same size as chars?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 10 '07 #17
Mark McIntyre <ma**********@s pamcop.netwrite s:
On Tue, 10 Jul 2007 14:40:52 +0200, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>>Eric Sosman wrote:
>>jacob navia wrote:

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.

Just curious: With what is the int-ness of 'A' "inconsiste nt?"

With it being a character of course.

Where does it say that characters are the same size as chars?
Yeah, how could *anyone* get confused by *that*?!?!?!?

<rolls eyes>

It was a dubious decision, for whatever reason, to default a character
constant to an int and not a char. Someone better versed in the history
gave a better point of view elsewhere in the thread.

Jul 10 '07 #18
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
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.
It makes very little difference in C; expressions of type char are
almost always promoted to int anyway. Applying sizeof to a character
constant is nearly the only case where the different shows up.

<OT>It matters more in C++ because of overloading; func('A') and
func(65) might call two different functions.</OT>

This is question 8.9 in the comp.lang.c FAQ.

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 10 '07 #19
Eric Sosman <Eric.Sos...@su n.comwrote:
jacob navia wrote On 07/10/07 08:40,:
Eric Sosman wrote:
jacob navia wrote:
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.
>
Just curious: With what is the int-ness
of 'A' "inconsiste nt?"
With it being a character of course.
... but since it *isn't* a character,
'A' could not possibly be thought of as character?!

Even if you 'know better', I can't believe the obviousness
of the expectation eludes you.

Note that C++ didn't make 'A' a char to support overloading.
That is incidental. C++ made 'A' a char because it's common
sense to think of and _use_ 'A' as a character. The
situation is no different in C.
you might just as well say its int-ness is inconsistent
with it being a struct tm.
If it had two distinct types simultaniously, I would say it
was inconsistent, yes.
The int-ness of 'A' may be surprising[*],
It is surprising.
but I can't think of any convention or practice elsewhere
in the language that establishes a pattern with which
it could be inconsistent.
Can you think of any convention or practice elsewhere in
the language that would established a pattern with which
'A' being a character would be inconsistent?
[*] Or maybe not, when you consider the usual
arithmetic conversions (6.3.1.8).
And why would making 'A' a character be inconsistent with
the usual arithmetic conversions? C++ programmers are no
more hampered in writing (*p - '0') than C programmers.
With the exception of sizeof, every operator[**] you
could apply to a char-type 'A' would promote its value
to int or unsigned int anyhow.
So let me turn that back at you. If it's going to happen
anyhow, again I ask, why would making 'A' a char cause
any harm?
[**] If I've overlooked something, I'm confident
someone will remind me.
Here's a question for you: If it's so obvious why 'A'
should be an int, why then does '\xfe' have no less
than 4 distinct possible values, depending on the
implementation?

C was designed for a purpose. Being a useful and
intuitive language for beginners wasn't one of them.
;-)

--
Peter

Jul 11 '07 #20

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

Similar topics

3
3562
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
2477
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
9258
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
3031
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
1947
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
2436
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2548
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
8574
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
2612
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
2911
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
10039
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
11359
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
11069
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,...
1
8102
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
7259
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5944
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
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4779
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
3
3370
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.