<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>unsigned char or  u8?</title>
		<link>http://www.allegro.cc/forums/view/599586</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 18 Mar 2009 06:59:29 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>From time to time I see people using u8 s8 u16 etc instead of the usual unsigned char etc.</p><p>What exacly is u8? is it just u8 defined as unsigned char?</p><p>Will using u32 instead of unsigned int make sure that the compiler on every system will leave it untouched?</p><p>Are there any downsides to using this system instead of the usual?</p><p>Thanks <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Sun, 15 Mar 2009 16:25:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There should be some typedefs for integral types of fixed type in <a href="http://dinkumware.com/manuals/?manual=compleat&amp;page=stdint.html">cstdint</a> (although probably not all compilers have this).</p><p>You can be quite sure that u8 is typedeffed as unsigned char (since the size of char is guaranteed to be 1). However, there are no guarantees for the other types. So it you typedef unsigned int as u32, there is no guarantee that it will be actually 4 bytes with each compiler. (If I&#39;m not mistaken, each compiler implementation provides suitable typedefs in the cstdint header, so that you won&#39;t have to worry about the size of types with this particular compiler.)</p><p>On the other hand, I&#39;ve never been tempted to use fixed-sized integral types anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Sun, 15 Mar 2009 17:27:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I see, I guess I just have to make sure no complications will arise on different systems then.</p><p>Thanks <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.</p><p>EDIT:<br />I thought I would be smart and made my own definitions to make the code more readable:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define ubyte unsigned char</span>
<span class="p">#define byte signed char</span>

<span class="p">#define uint unsigned int</span>
<span class="p">#define int signed int</span>
</pre></div></div><p>

But as you can see this causes some problems as uint becomes both signed and unsigned, my question is, can I bypass this problem and if so, how?</p><p>The order of the definitions does not matter, apparently.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Sun, 15 Mar 2009 17:37:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I thought I would be smart and made my own definitions to make the code more readable
</p></div></div><p>

I think it becomes less readable, it&#39;s best to just use the normal C++ types. And in the (rare) cases where you need fixed bit sizes, use cstdint (stdint.h in C99), no need to #define your own.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sun, 15 Mar 2009 18:09:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>    <span class="k1">signed</span> <span class="k1">char</span> <span class="k3">*</span>value_name<span class="k2">;</span>
    <span class="k1">signed</span> <span class="k1">int</span> <span class="k3">*</span>value_subtopic<span class="k2">;</span>
    <span class="k1">signed</span> <span class="k1">char</span> <span class="k3">*</span><span class="k3">*</span>subtopic<span class="k2">;</span>

    <span class="k1">signed</span> <span class="k1">int</span> <span class="k3">*</span>integer_values<span class="k2">;</span>
    <span class="k1">double</span> <span class="k3">*</span>double_values<span class="k2">;</span>
</pre></div></div><p>

I think this is very irritating to read, but It&#39;s personal taste of course.</p><p>I&#39;ll have to do it this way I guess :/.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Sun, 15 Mar 2009 18:29:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A plain &#39;int&#39; is always signed.  Never write &#39;signed int&#39;, it&#39;s just a waste of space.</p><p>The only reason ever to use the &#39;signed&#39; keyword is with chars, since they can be either signed or unsigned by default.  If you&#39;re using chars for small integers, as opposed to for characters, it&#39;s common to use &#39;unsigned char&#39; or &#39;signed char&#39;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sun, 15 Mar 2009 18:58:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How sure are you about &quot;always&quot;?</p><p>Thanks.</p><p>EDIT: I mean, if int is always signed then why can I write signed int?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Sun, 15 Mar 2009 19:16:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, I am sure.</p><p>I don&#39;t know why they chose to add signed as a keyword with only a single use case, but that&#39;s what it is.  Seems a bit silly in hindsight.  Probably &#39;historical reasons&#39;.  Or they felt it made such a nice pair together with unsigned... <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sun, 15 Mar 2009 20:02:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, thanks, from now on I&#39;ll program as if that&#39;s true.</p><p>Wierd <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Sun, 15 Mar 2009 20:31:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You can be quite sure that u8 is typedeffed as unsigned char (since the size of char is guaranteed to be 1).
</p></div></div><p>
Bzzzzt!<br />sizeof measures size in units of a char. That doesn&#39;t say a char has to be 8 bits.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sun, 15 Mar 2009 20:53:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Evert said:</div><div class="quote"><p>

Bzzzzt!<br />sizeof measures size in units of a char. That doesn&#39;t say a char has to be 8 bits.
</p></div></div><p> Really? Interesting.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Sun, 15 Mar 2009 22:27:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Some people prefer typedefs for integral types (use a typedef, not a #define).  I think it&#39;s easier to read and deal with, personally.  Most of my projects will have a types header that goes along the lines of </p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;boost/cstdint.hpp&gt;</span>

<span class="k1">typedef</span> <span class="k1">int</span>                     SInt<span class="k2">;</span>
<span class="k1">typedef</span> <span class="k1">unsigned</span> <span class="k1">int</span>            UInt<span class="k2">;</span>
<span class="k1">typedef</span> boost::int_least8_t     SInt8<span class="k2">;</span>
<span class="k1">typedef</span> boost::uint_least8_t    UInt8<span class="k2">;</span>
<span class="k1">typedef</span> boost::int_least16_t    SInt16<span class="k2">;</span>
<span class="k1">typedef</span> boost::uint_least16_t   UInt16<span class="k2">;</span>
<span class="k1">typedef</span> boost::int_least32_t    SInt32<span class="k2">;</span>
<span class="k1">typedef</span> boost::uint_least32_t   UInt32<span class="k2">;</span>
<span class="k1">typedef</span> boost::int_least64_t    SInt64<span class="k2">;</span>
<span class="k1">typedef</span> boost::uint_least64_t   UInt64<span class="k2">;</span>

<span class="p">#define nullptr 0</span>
</pre></div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Really? Interesting.
</p></div></div><p>

Indeed.  Essentially all modern PC platforms will have an 8 bit byte/char size, but there are other platforms that C/C++ can be used on where the size differs.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Speedo)</author>
		<pubDate>Mon, 16 Mar 2009 04:37:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Most of my projects will have a types header that goes along the lines of
</p></div></div><p>Only ones that think its a good idea to produce harder to read code, and duplicate types for no reason what so ever.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 16 Mar 2009 10:38:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah, so that&#39;s what typedef is used for.. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />.</p><p>Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Mon, 16 Mar 2009 16:55:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Only ones that think its a good idea to produce harder to read code, and duplicate types for no reason what so ever.
</p></div></div><p>

You do know that expressing your opinions in a way that make you look like an arrogant <span class="cuss"><span>ass</span></span> is the best way to make people ignore you... right?  <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Speedo)</author>
		<pubDate>Tue, 17 Mar 2009 00:59:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You do know that expressing your opinions in a way that make you look like an arrogant <span class="cuss"><span>ass</span></span> is the best way to make people ignore you... right? <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div></div><p>
He does have a point, you do realise that, right?<br />There are standard datatypes. Use them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Tue, 17 Mar 2009 04:25:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
He does have a point, you do realise that, right?<br />There are standard datatypes. Use them.
</p></div></div><p>

Then you shouldn&#39;t have a problem telling me which integer type to use that will be at least 32 bits wide on every platform.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Speedo)</author>
		<pubDate>Tue, 17 Mar 2009 06:22:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>(u)int32_t is available on every C99 compiler, and some C89 compilers that like to include bits of later standards.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 17 Mar 2009 07:51:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
(u)int32_t is available on every C99 compiler, and some C89 compilers that like to include bits of later standards.
</p></div></div><p>

C99 != C++
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Speedo)</author>
		<pubDate>Tue, 17 Mar 2009 08:52:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
C99 != C++
</p></div></div><p>
It might still work though.<br />In either case, you can provide it yourself by picking a standard name rather than making up your own.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Tue, 17 Mar 2009 09:48:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Then you shouldn&#39;t have a problem telling me which integer type to use that will be at least 32 bits wide on every platform.</p></div></div><p>

long is guaranteed to at least 32 bits.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Tue, 17 Mar 2009 18:19:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t think long or any other C++ datatypes have a guaranteed bit size - as Evert pointed out, the standard does not even require a char to be 8 bits. What you want is int_least32_t.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 17 Mar 2009 18:25:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Wrong, the types integer types have guaranteed minimum types.  Look it up.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Tue, 17 Mar 2009 18:28:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I did, but I only read the &quot;simple types&quot; or whatever it is called section of the standard. I assumed it would mention bits there if it does at all...</p><p>[edit:] &quot;Fundamental Types&quot; it was. And no, the only occurrence of the number &quot;32&quot; in the C++ standard is to tell that std::atexit() must support registering at least 32 functions <img src="http://www.allegro.cc/forums/smileys/tongue.gif" /></p><p>I only have a draft though, so maybe it changed in the final version?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 17 Mar 2009 18:52:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The C++ standard doesn&#39;t require minimum sizes, but it does require minimum ranges - see <a href="http://home.att.net/~jackklein/c/inttypes.html">this page</a>. When applied to a 2&#39;s complement machine (like pretty much every single machine currently in use), those ranges translate to the following minimum type sizes:<br />char: 8 bits<br />short: 16<br />int: 16<br />long: 32<br />Also, it is a recommendation (but not a requirement) that a char corresponds to the smallest unit a machine can address, and that an int is the &#39;native&#39; size of the platform. As the article states, an implementation meets the standard by making all 4 integer types 32 bits wide.<br />The number 32 doesn&#39;t appear because the standard requires a range of at least -32767 ... 32767.<br />Minimum sizes are generally fine, unless you do silly things like brute-force pointer casts, e.g.:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> i <span class="k3">=</span> <span class="n">12345</span><span class="k2">;</span>
<span class="k1">char</span><span class="k3">*</span> c <span class="k3">=</span> <span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span><span class="k2">(</span><span class="k3">&amp;</span>i<span class="k2">)</span><span class="k2">;</span>
<span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> j <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> j <span class="k3">&lt;</span> <span class="n">4</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>j<span class="k2">)</span>
  c<span class="k2">[</span>j<span class="k2">]</span> <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
</pre></div></div><p>
The above obviously breaks when sizeof(char) * 4 &gt; sizeof(int).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Tue, 17 Mar 2009 19:36:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Wrong, the types integer types have guaranteed minimum types. Look it up.</p></div></div><p>
Yes, they do. A &quot;char&quot; is size 1 by definition, a short is at least as large as a char, an int is at least as large as a short and should correspond to the machine&#39;s native word size and a long is at least the size of an int.<br />They could all be 8 bits or 32 bits as far as the standard is concerned (and historically have been on different architectures).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Tue, 17 Mar 2009 19:51:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, that&#39;s how I read it, they could all be 8-bit (of course, in practice, as many compilers will do that as use 9-bit char <img src="http://www.allegro.cc/forums/smileys/tongue.gif" />). But what it means is, if you need a minimum number of bits, it&#39;s best to use something like int_least32_t. Usually you don&#39;t and then int is fine.</p><p>[Edit:] We are talking about C++ here btw, <b>not</b> C99 which is different (and which that home.att.net site linked above seems to talk about).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 17 Mar 2009 19:56:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just for fun, here&#39;s a quote from &quot;The C++ Programming Language, special edition&quot;, §4.6 for you:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>In addition, is is guaranteed that a char has at least 8 bits, a short at least 16 bits, and a long at least 32 bits.
</p></div></div><p>

It might not be the standard, but Stroustroup tends to get his facts right. He also probably has a copy of the standard on his shelf, which I don&#39;t. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" /></p><p>The minimum 16 bit shorts ands ints, and 32 bit longs, are mentioned in K&amp;R 1989 edition.  I didn&#39;t bother trying to find a note about char sizes.</p><p>EDIT: Of course, if you&#39;re programming an 8-bit cpu, the C compiler might not follow the standard in regards to minimum sizes.  I wouldn&#39;t be surprized if C compilers that use 8 bit ints exist.  And maybe there&#39;s no floating point support at all, etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 18 Mar 2009 02:13:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, if that book was published before 1998, the author definitely did not have a copy of the standard <img src="http://www.allegro.cc/forums/smileys/tongue.gif" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 18 Mar 2009 06:05:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Good point, edition I&#39;ve got is from 1997.  Although he was actually on the committe, and probably had a bunch of drafts laying about.  Besides, I have the 2002 printing. <img src="http://www.allegro.cc/forums/smileys/cool.gif" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 18 Mar 2009 06:46:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I figured out the problem - the C++ standard (neither the one from 1998, nor the updated one from 2003, nor the upcoming C++v0) does not specify any minimum sizes <i>within the text of the standard</i>. But, all the above three reference the C90 standard (C++v0 may or may not reference C99 instead from what I understand) and so they inherit minimum sizes from there.</p><p>So what you said is right - int is at least 16 bit and long at least 32 bit. However you cannot look that up in the C++ standard, only in the C standard referenced by it <img src="http://www.allegro.cc/forums/smileys/tongue.gif" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 18 Mar 2009 06:59:29 +0000</pubDate>
	</item>
</rss>
