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 7203
Peter Nilsson <ai***@acay.com .auwrites:
Eric Sosman <Eric.Sos...@su n.comwrote:
[...]
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.
<OT>
According to the C++ standard, C++ made character constants type char
precisely because of overloading. See Annex C of the 1998 C++
standard:
Change: Type of character literal is changed from int to char
Rationale: This is needed for improved overloaded function
argument type matching.
</OT>
>The int-ness of 'A' may be surprising[*],
It is surprising.
Agreed (though I got over the surprise some time ago).
>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?
Yes. 'A' is a constant. No other constants are of any integer type
smaller than int. For example, the macro CHAR_MAX typically expands
to either 127 or 255; both of these are constants of type int. The
language provides suffixes for integer constants that make them of
type long, unsigned int, unsigned long, and so forth, but there are no
such suffixes for char, unsigned char, signed char, short, or unsigned
short.
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof". Why should character constants be a special case?
The only case where it makes any difference is when a character
constant is the operand of sizeof. How often do you really apply
sizeof to a character constant in real code (unless you're trying to
determine whether you're using a C or C++ compiler)?
--
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"
Keith Thompson said:
<snip>
How often do you really apply
sizeof to a character constant in real code (unless you're trying to
determine whether you're using a C or C++ compiler)?
And, quite possibly, failing, since sizeof 'A' can certainly be 1, even
though 'A' is an int.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Peter Nilsson wrote:
[...]
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?
What type should 'AB' have?
Yes, it's implementation-defined. And yes, because of
its implementation-definedness it is (justifiably) denigrated.
But to deny its existence -- and occasional usefulness -- is
to deny the history of C. (Ritchie's writings on the descent
of C from B and the evanescent NB make illuminating reading.)
True, there's precedent for the value of a constant to
determine its type, over and above its lexical appearance:
42 is an int, but 424242424242424 242424242424242 42424 quite
likely isn't. But that's not really a "pre"cedent , since
it's a rule that was invented long after C began, a rule
that describes rather than prescribes.
Other than offering newbies a surprise -- and let's face
it, there are *many* things about C that surprise newbies --
can you think of even one deleterious consequence of 'A' being
an int?
--
Eric Sosman es*****@ieee-dot-org.invalid
Keith Thompson wrote:
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof".
There's no promotion when the left and right operands
of the assignment operator are of type char.
There's no promotion when an expression of type char
is added to a pointer.
The logical operators don't cause the promotion
of their operands,
and neither does the comma operator.
It's not like for an array name.
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
char a = 0;
char array[7] = "";
printf("sizeof (a , a) is %u\n", (unsigned)sizeo f (a , a));
printf("sizeof ( a) is %u\n\n", (unsigned)sizeo f ( a));
printf("sizeof (array , array) is %u\n",
(unsigned)sizeo f (array , array));
printf("sizeof ( array) is %u\n",
(unsigned)sizeo f ( array));
return 0;
}
/* END new.c */
--
pete
Eric Sosman <Er*********@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,
Yes, it is. 'A' is a character constant. It is not a character type.
That's the inconsistency: character constants don't have character type,
but type int.
Richard
Eric Sosman <es*****@ieee-dot-org.invalidwrot e:
Peter Nilsson wrote:
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?
What type should 'AB' have?
Never mind type, what _value_ should it have? IMO, none. Character
constants should be limited to a single member of the execution
character set.
Richard
Keith Thompson <ks***@mib.orgw rote: rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Nishu <na**********@g mail.comwrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:
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.
Oh, I know that; but it irks me for philosophical reasons.
Richard
On Jul 11, 10:04 am, pete <pfil...@mindsp ring.comwrote:
Keith Thompson wrote:
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof".
There's no promotion when the left and right operands
of the assignment operator are of type char.
There's no promotion when an expression of type char
is added to a pointer.
The logical operators don't cause the promotion
of their operands,
and neither does the comma operator.
This appplies to a cast expression and also in the case, when it is a
primary expression.
That is:
unsigned char c='A';
(unsigned)c;
c;
(c);
/*In the last 3 expression statements, there need to be no promotion,
although having promotion doesn't affect the behavior of the code.*/
In general, integer promotion occurs only when the operand having rank
less that int, is an operand of an arithmetic operator.
>
It's not like for an array name.
What are you trying to prove with array names?
Array names are always converted into an rvalue equal to the pointer
to the first element except when it is an operand of sizeof or &.
Note that array names are NOT promoted. The pointer value you get with
array name is analogous to the char value you get when you have an
identifier of type char, in rvalue context. Promotion is what, that
subsequently occurs, when char operand is an operand of an arithmetic
operator (need not occur otherwise).
Note that,for array names there is no "promotion" defined by the
standards. If you are refferring to evaluation of array name as
pointer, then that is incorrect terminology!
>
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
char a = 0;
char array[7] = "";
printf("sizeof (a , a) is %u\n", (unsigned)sizeo f (a , a));
printf("sizeof ( a) is %u\n\n", (unsigned)sizeo f ( a));
printf("sizeof (array , array) is %u\n",
(unsigned)sizeo f (array , array));
printf("sizeof ( array) is %u\n",
(unsigned)sizeo f ( array));
return 0;
}
/* END new.c */
pete <pf*****@mindsp ring.comwrites:
Keith Thompson wrote:
>All C expressions of integer type are of type int, unsigned int, or something larger. Even the name of an object of type char is promoted to int (or, very rarely, to unsigned int) unless it's the operand of "&" or "sizeof".
There's no promotion when the left and right operands
of the assignment operator are of type char.
There's no promotion when an expression of type char
is added to a pointer.
The logical operators don't cause the promotion
of their operands,
and neither does the comma operator.
[snip]
Ok, you're right. Here's another good illustration of your point:
#include <stdio.h>
int main(void)
{
char c;
printf("sizeof c = %d\n", (int)sizeof c);
printf("sizeof (c, c) = %d\n", (int)sizeof (c, c));
printf("sizeof (c + 0) = %d\n", (int)sizeof (c + 0));
return 0;
}
The output I get (on a system with sizeof(int)==4) is:
sizeof c = 1
sizeof (c, c) = 1
sizeof (c + 0) = 4
It's still the case that it hardly ever matters whether character
constants are of type int or of type char.
--
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" rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Eric Sosman <Er*********@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,
Yes, it is. 'A' is a character constant. It is not a character type.
That's the inconsistency: character constants don't have character type,
but type int.
Richard
Which is the crux of the issue. I find it hard to believe that anyone,
even in this group, would argue that a character constant has type int
is a good thing. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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"; }
|
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>
|
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));
|
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
| |
by: Christopher C. Stacy |
last post by:
Some people say sizeof(type) and other say sizeof(variable).
Why?
|
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));
|
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
|
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
|
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);
|
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...
| |
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...
|
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,...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
| |
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
|
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...
| |