473,795 Members | 2,922 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 7206
pete <pf*****@mindsp ring.comwrites:
Keith Thompson wrote:
>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;
}

I think that (c + c) would be a better example of integer promotions,
than (c + 0) is,
because even if there were no such thing as "integer promotions",
the "usual arithmetic conversions" would still operate to make
(c + 0) be an expression of type int.
Agreed.

--
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 11 '07 #41
On Jul 11, 11:51 pm, Richard <rgr...@gmail.c omwrote:
'AB' is not "a character" in any sense.
Except for the sense covered by section
6.4.4.4 of the C standard.

Jul 11 '07 #42
CryptiqueGuy wrote:
In general, integer promotion occurs only when the operand having rank
less that int, is an operand of an arithmetic operator.
Shift operators do integer promotions.

--
pete
Jul 12 '07 #43
pete <pf*****@mindsp ring.comwrites:
CryptiqueGuy wrote:
>In general, integer promotion occurs only when the operand having rank
less that int, is an operand of an arithmetic operator.

Shift operators do integer promotions.
Isn't a shift operator an arithmetic operator? I am honestly
curious about the distinction you are making here, which is not
clear to me.
--
"I should killfile you where you stand, worthless human." --Kaz
Jul 12 '07 #44
pete wrote:
>
CryptiqueGuy wrote:
In general, integer promotion occurs only when
the operand having rank
less that int, is an operand of an arithmetic operator.

Shift operators do integer promotions.
Actually, there's a whole bunch of operators,
besides the arithmetic ones,
that cause the usual arithmetic conversions.
The usual arithmetic conversions
include the integer promotions.

--
pete
Jul 12 '07 #45
Ben Pfaff wrote:
>
pete <pf*****@mindsp ring.comwrites:
CryptiqueGuy wrote:
In general, integer promotion occurs only when the operand having rank
less that int, is an operand of an arithmetic operator.
Shift operators do integer promotions.

Isn't a shift operator an arithmetic operator? I am honestly
curious about the distinction you are making here, which is not
clear to me.
I consider it to be a bitwise operator.
Do you consider bitwise operators to arithmetic operators?

--
pete
Jul 12 '07 #46
pete wrote:
>
Ben Pfaff wrote:

pete <pf*****@mindsp ring.comwrites:
CryptiqueGuy wrote:
>In general, integer promotion occurs only when the operand having rank
>less that int, is an operand of an arithmetic operator.
>
Shift operators do integer promotions.
Isn't a shift operator an arithmetic operator? I am honestly
curious about the distinction you are making here, which is not
clear to me.

I consider it to be a bitwise operator.
Do you consider bitwise operators to arithmetic operators?
K&R2 has arithmetic operators in section 2.5
and bitwise operators in section 2.9.

The index of the C99 standard, groups shift operators
and bitwise operators under the entry for "arithmetic operators",
but the C90 standard index, doesn't.

--
pete
Jul 12 '07 #47
pete <pf*****@mindsp ring.comwrites:
Ben Pfaff wrote:
>Isn't a shift operator an arithmetic operator? I am honestly
curious about the distinction you are making here, which is not
clear to me.

I consider it to be a bitwise operator.
Do you consider bitwise operators to arithmetic operators?
Ah, I understand what you are saying now. I am not sure whether
I agree or disagree. After all, I most often use shift operators
to do arithmetic, and their behavior is defined arithmetically.
But I definitely understand their operation in terms of their
effect on bits, too. I think that it may be a gray area.
Ultimately it is not too important, I guess.

C99's index has bitwise operators, shift operators, and other
kinds of operators as subcategories under "arithmetic
operators".
--
In personal news, I passed my PhD oral exam this afternoon. So I
will be receiving my PhD pretty soon, probably by the end of
September, as soon as my thesis is accepted.
Jul 12 '07 #48
Eric Sosman <esos...@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?
I think char, but my opinion is irrelevant. [I also think
C should have a separate byte type, and that char should
be implementation defined, only come in the plain varienty,
and be unsigned (like size_t); but that's not likely to
get through the Committee, whether the concept has merit
or not.]

<snip>
Other than offering newbies a surprise -- and
let's face it, there are *many* things about C that
surprise newbies
Plenty of experienced programmers examining C for the
first time are surprised by the fact too. These days,
more and more programmers are encountering C++ before
the encounter C.
-- can you think of even one deleterious consequence of
'A' being an int?
I think '\xfe' having 4 different values is more of a
concern to me that it's type. Happy to agree that 'A'
being an int isn't really an issue for C programmers...
presently... 'user' defined type generic functions may
change that.

But the issue that sparked the sub thread was your
argument that character constants not having a character
type should not come as a surprise (to anyone).

Even if it's no more significant than the so-called
birthday paradox, it's still a surprise to many people.

--
Peter

Jul 12 '07 #49
Peter Nilsson wrote On 07/11/07 22:06,:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>>-- can you think of even one deleterious consequence of
'A' being an int?


I think '\xfe' having 4 different values is more of a
concern to me that it's type. [...]
'\xfe' is always 254 on systems where the behavior is
defined, that is, on systems where CHAR_MAX >= 254. On
other systems the behavior is undefined and there's no
reason to restrict the outcome to just three other values.
Could be any value at all, or nasal demons.

Argument for undefinedness:

6.4.4.4p6 says "[...] The numerical value of the
hexadecimal integer so formed specifies the value of
the desired character or wide character." The specified
value is therefore 254.

6.4.4.4p10 says "[...] If an integer character constant
contains a single character or escape sequence, its value is
the one that results when an object with type char whose
value is that of the single character or escape sequence is
converted to type int." Hence, the value of '\xfe' is the
result of converting a char object with value 254 to int.

On a system where CHAR_MAX < 254 no char object can
have the value 254, so 6.4.4.4p10 does not specify the
value of '\xfe' on those systems.

4p2 says "[...] Undefined behavior is otherwise
indicated [...] by the omission of any explicit definition
of behavior. There is no difference in emphasis among these
three; they all describe ``behavior that is undefined.''"
Since the derivation of an int value from '\xfe' is not
explicitly defined when CHAR_MAX < 254, the behavior is
undefined.

Observation: The int-ness of character constants is not
to blame for this situation. If they were of type char and
the conversion language were deleted from 6.4.4.4p10, you'd
still be stuck with the fact that no char can have a value
greater than CHAR_MAX. The value of '\xfe' (if it has one)
is not germane to the debate over character constants' types.

--
Er*********@sun .com
Jul 12 '07 #50

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
2539
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
8496
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
2593
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
9519
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
10439
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
10165
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
10001
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...
0
9043
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, 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...
0
6783
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
5437
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...
1
4113
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
2920
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.