I read in K&R page 204 that sizeof use on a char returns 1. But when I write
the following I get 4!
int main(void){
printf("%d\n",s izeof('g'));
}
Was it not supposed to return 1?
Nov 14 '05
36 2285
In article <3a************ *@individual.ne t>, Nils Weller wrote: In article <87************ @benpfaff.org>, Ben Pfaff wrote: Nils Weller <me@privacy.net > writes:
Because character constants were designed to be capable of carrying more more than one character (and it follows, obviously, that a ``char'' is not enough to carry more than one character - hence the need for a larger data type.)
Okay--so, then, *why* were character constants designed to be capable of carrying more than one character?
That I don't know. I'd guess that it was intended to automize bit shifting for you. The value of a multi-char character constant is now implementation-defined, but historically you really got all chars you asked for:
'foo'
would turn into
'f' << (CHAR_BIT * 2) | 'o' << CHAR_BIT | 'o'
This may seem more plausible for values written in hexadecimal notation:
'\xff\x12'
would turn into
0xff << CHAR_BIT | 0x12
Actually it makes a lot more sense for values written in octal notation
since 0xff << CHAR_BIT | 0x12 can be represented as a simple 0xff12 as
well.
--
My real email address is ``nils<at>gnuli nux<dot>nl''
JS wrote on 23/03/05 : I read in K&R page 204 that sizeof use on a char returns 1. But when I write
Correct.
the following I get 4!
int main(void){ printf("%d\n",s izeof('g'));
}
Was it not supposed to return 1?
No, because a character literal is not a char. It fits into a char. Big
difference.
Actually, a character literal is an int.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
Mark Odell wrote on 23/03/05 : char c; printf("%d\n", (int)sizeof c);
Don't you mean: printf("%u\n", sizeof c);
return 0; }
Assuming <stdio.h> was included, the first version was correct. Yours
is not, because a size_t could be an unsigned long. You want :
printf("%u\n", (unsigned) sizeof c);
or (C99)
printf("%zu\n", sizeof c);
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"C is a sharp tool"
Steven K. Mariner wrote on 23/03/05 : char p[]="ABCDEFGHIJKLM NOPQRSTUVWXY*Za bcdefghijklmnop qrstuvwxyz. \n",*q="kl BIcNBFr.NKEzjwC IxNJC";
Put that on a single line, and cut at the comma...
char p[]="ABCDEFGHIJKLM NOPQRSTUVWXY*Za bcdefghijklmnop qrstuvwxyz.\n"
,*q="kl BIcNBFr.NKEzjwC IxNJC";
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"C is a sharp tool"
Martin Ambuhl wrote: Mark Odell wrote:
Alex Fraser wrote:
No, a character in single quotes, as in 'g', is a constant of type signed int in C (I think this is different in C++).
Try:
int main(void) { char c; printf("%d\n", (int)sizeof c); Don't you mean: printf("%u\n", sizeof c);
No, since there is no reason to think that a size_t is an unsigned int. The "correct" specifier for an uncast size_t might be "%lu" or "%llu".
Addendum:
In C99, we have "%zu" specifically for size_t.
No more guesswork :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nils Weller <me@privacy.net > writes: In article <d1**********@c hessie.cirr.com >, Christopher Benson-Manica wrote: Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:
In C, character constants have type `int', so sizeof 'g' is equal to sizeof(int).
(semi OT) Why did the authors of the standard make this arguably non-intuitive stipulation?
Because you may be able to put more than one character into character constants (but the actual number that fits into it is implementation- defined any may be 1.)
putchar('foo');
[...]
I don't think that's the reason.
In C, especially in older versions such as K&R C, there are few if any
expressions of integer types shorter than int. Evaluating an object
of type char or short almost always causes an implicit conversion to
int. (I might be wrong about the "almost".) Making character
constants have type char would actually make things more complicated.
For example:
char c;
c = 'g';
If 'g' were of type char, the value would be implicitly converted to
type int before being converted back to type char and assigned to c.
With 'g' being of type int, there's only one implicit conversion, not
two. (Of course the compiler is likely to optimize the whole thing to
a single "store byte" instruction, if such an instruction exists.)
The only case I can think of where this visibly matters is applying
the sizeof operator to a character constant, something that is rarely
useful.
<OT>
In C++, character constants are of type char; this is because of some
considerations involving overloading and/or templates, which don't
apply in C.
</OT>
Incidentally, the OP's question could have been answered by citing
question 8.9 in the 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.
<posted & mailed>
sizeof() returns the storage size of the argument specified. Since 'g' is an
integer constant, you get sizeof(int). On your platform... that's 4.
JS wrote: I read in K&R page 204 that sizeof use on a char returns 1. But when I write the following I get 4!
int main(void){ printf("%d\n",s izeof('g'));
}
Was it not supposed to return 1?
--
Remove '.nospam' from e-mail address to reply by e-mail
On Wed, 23 Mar 2005 18:08:25 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote in comp.lang.c: Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:
In C, character constants have type `int', so sizeof 'g' is equal to sizeof(int).
(semi OT) Why did the authors of the standard make this arguably non-intuitive stipulation?
Let's just assume ASCII for a moment, to simplify:
char c1 = 65;
char c2 = 'A';
What is the type of the initializer for c1? What is the type of the
initializer for c2? Why should they not be the same type? Either can
fit in a char.
....or even more succinctly:
char c1 = 0;
char c2 = '\0';
Note that all constant expressions of integral type are of type int or
higher rank. The rules for character literals are no different than
those for other integer literals. Unsuffixed integer literals that
have representable in an int are of type int, not of the smallest type
that can hold the value.
Consider that the only differences between 'A', 65, 0x41, and 081
exist syntactically in the way they are parsed at run time. They are
all one and exactly the same thing at run time, integer literals of
type int.
This is all very consistent with C's basic philosophy that there is
nothing special about characters, they are just integer types holding
numbers. As opposed to languages where one has to use special
functions (CHR$ and ASC come to mind from old interpreted BASIC
dialects) to handle the conversion between characters and their
numeric values.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jack Klein wrote: On Wed, 23 Mar 2005 18:08:25 +0000 (UTC), Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote in comp.lang.c:
Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:
In C, character constants have type `int', so sizeof 'g' is equal to sizeof(int). (semi OT) Why did the authors of the standard make this arguably non-intuitive stipulation?
Let's just assume ASCII for a moment, to simplify:
char c1 = 65; char c2 = 'A';
What is the type of the initializer for c1? What is the type of the initializer for c2? Why should they not be the same type? Either can fit in a char.
...or even more succinctly:
char c1 = 0; char c2 = '\0';
Note that all constant expressions of integral type are of type int or higher rank. The rules for character literals are no different than those for other integer literals. Unsuffixed integer literals that have representable in an int are of type int, not of the smallest type that can hold the value.
Consider that the only differences between 'A', 65, 0x41, and 081 exist syntactically in the way they are parsed at run time. They are
ITYM: compile time (here, as opposed to run time below).
all one and exactly the same thing at run time, integer literals of type int.
This is all very consistent with C's basic philosophy that there is nothing special about characters, they are just integer types holding numbers. As opposed to languages where one has to use special functions (CHR$ and ASC come to mind from old interpreted BASIC dialects) to handle the conversion between characters and their numeric values.
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jack Klein <ja*******@spam cop.net> spoke thus: ...or even more succinctly:
char c1 = 0; char c2 = '\0';
That was, indeed, very succinct. Thanks for a highly enlightening
article :)
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roy Yao |
last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ?
According to my analysis, operator sizeof, (type) and * have the same
precedence, and they combine from right to left. Then this expression should
equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why?
Can anyone help me? Thanks.
Best regards.
Roy
|
by: Anjali M |
last post by:
#include <stdio.h>
int main()
{
int number = 10;
printf("Number is: %d\n", number);
printf("Sizeof of number is: %d\n", sizeof(number++));
printf("Number now is: %d\n", number);
|
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: ozbear |
last post by:
If one were writing a C interpreter, is there anything in the standard
standard that requires the sizeof operator to yield the same value for
two different variables of the same type?
Let's assume that the interpreter does conform to the range values
for, say, type int, but allocates storage for the variables based
on their value. So, for two variables foo and bar
int foo = 0; /* interpreter allocates two bytes */
int bar =...
|
by: sonu |
last post by:
#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);
printf("x=%d\ny=%d\n",x,y);
}
Oput Put
| |
by: Alex Vinokur |
last post by:
Why does one need to use two kinds of sizeof operator:
* sizeof unary-expression,
* sizeof (type-name)
?
Their behavior seem not to be different (see an example below).
------ C++ code ------
#include <iostream>
using namespace std;
|
by: Richard |
last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
|
by: Howard Bryce |
last post by:
I have come across code containing things like
sizeof int
How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?
|
by: Frederick Gotham |
last post by:
sizeof has the same precedence as a cast, and they both bind from right to
left. The following won't compile for me with gcc:
int main(void)
{
sizeof(double)5;
return 0;
}
|
by: Divick |
last post by:
Hi,
does sizeof operator evaluate the size at compile time or run time
? In other words is sizeof(SomeType) evaluated at compile time or at
runtime? Please provide the reasoning involved as well.
TIA,
Divick
|
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,...
| |
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...
|
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: 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: 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...
|
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
| |