/usr/share/doc/gccintro/gccintro/Portability-of-signed-and-unsigned-types.html is in gccintro 1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | <html lang="en">
<head>
<title>Portability of signed and unsigned types - An Introduction to GCC</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="An Introduction to GCC">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Platform_002dspecific-options.html#Platform_002dspecific-options" title="Platform-specific options">
<link rel="prev" href="Floating_002dpoint-issues.html#Floating_002dpoint-issues" title="Floating-point issues">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Portability-of-signed-and-unsigned-types"></a>
<p>
Previous: <a rel="previous" accesskey="p" href="Floating_002dpoint-issues.html#Floating_002dpoint-issues">Floating-point issues</a>,
Up: <a rel="up" accesskey="u" href="Platform_002dspecific-options.html#Platform_002dspecific-options">Platform-specific options</a>
<hr>
</div>
<h3 class="section">9.7 Portability of signed and unsigned types</h3>
<p><a name="index-signed-_0040code_007bchar_007d-option-692"></a><a name="index-unsigned-_0040code_007bchar_007d-option-693"></a><a name="index-g_t_0040code_007bchar_007d_002c-portability-of-signed-vs-unsigned-694"></a>
The C and C++ standards allows the character type <code>char</code> to be
signed or unsigned, depending on the platform and compiler. Most
systems, including x86 GNU/Linux and Microsoft Windows, use signed
<code>char</code>, but those based on PowerPC and <span class="sc">arm</span> processors
typically use unsigned <code>char</code>.<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a> This can lead to unexpected results when porting
programs between platforms which have different defaults for the type
of <code>char</code>.
<p>The following code demonstrates the difference between platforms with
signed and unsigned <code>char</code> types:
<pre class="example"><pre class="verbatim"> #include <stdio.h>
int
main (void)
{
char c = 255;
if (c > 128) {
printf ("char is unsigned (c = %d)\n", c);
} else {
printf ("char is signed (c = %d)\n", c);
}
return 0;
}
</pre></pre>
<p class="noindent">With an unsigned <code>char</code>, the variable <code>c</code> takes the value 255,
but with a signed <code>char</code> it becomes -1.
<p>The correct way to manipulate <code>char</code> variables in C is through the
portable functions declared in <samp><span class="file">ctype.h</span></samp>, such as <code>isalpha</code>,
<code>isdigit</code> and <code>isblank</code>, rather than by their numerical
values. The behavior of non-portable conditional expressions such as
<code>c > 'a'</code> depends on the signedness of the <code>char</code> type. If
the signed or unsigned version of <code>char</code> is explicitly required at
certain points in a program, it can be specified using the declarations
<code>signed char</code> or <code>unsigned char</code>.
<p>For existing programs which assume that <code>char</code> is signed or
unsigned, GCC provides the options <samp><span class="option">-fsigned-char</span></samp> and
<samp><span class="option">-funsigned-char</span></samp> to set the default type of <code>char</code>. Using
these options, the example code above compiles cleanly when <code>char</code>
is unsigned:
<a name="index-g_t_0040option_007b_002dfunsigned_002dchar_007d-option-695"></a><a name="index-g_t_0040option_007b_002dfsigned_002dchar_007d-option-696"></a>
<pre class="example"> $ gcc -Wall -funsigned-char signed.c
$ ./a.out
char is unsigned (c = 255)
</pre>
<p class="noindent">However, when <code>char</code> is signed the value 255 wraps around to
-1, giving a warning:
<pre class="example"> $ gcc -Wall -fsigned-char signed.c
signed.c: In function `main':
signed.c:7: warning: comparison is always false due to
limited range of data type
$ ./a.out
char is signed (c = -1)
</pre>
<p class="noindent">The warning message <cite>“comparison is always true/false due to
limited range of data type”</cite> is one symptom of code which assumes a
definition of <code>char</code> which is different from the actual type.
<p>The most common problem with code written assuming signed <code>char</code>
types occurs with the functions <code>getc</code>, <code>fgetc</code> and
<code>getchar</code> (which read a character from a file). They have a return
type of <code>int</code>, not <code>char</code>, and this allows them to use the
special value -1 (defined as <code>EOF</code>) to indicate an
end-of-file error. Unfortunately, many programs have been written which
incorrectly store this return value straight into a <code>char</code>
variable. Here is a typical example:
<pre class="example"><pre class="verbatim"> #include <stdio.h>
int
main (void)
{
char c;
while ((c = getchar()) != EOF) /* not portable */
{
printf ("read c = '%c'\n", c);
}
return 0;
}
</pre></pre>
<p class="noindent">This only works on platforms which default to a signed <code>char</code>
type.<a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a>
On platforms which use an unsigned <code>char</code> the same code will fail,
because the value -1 becomes 255 when stored in an <code>unsigned
char</code>. This usually causes an infinite loop because the end of the file
cannot be recognized.<a rel="footnote" href="#fn-3" name="fnd-3"><sup>3</sup></a>
To be portable, the program should test the
return value as an integer before coercing it to a <code>char</code>, as
follows:
<pre class="example"><pre class="verbatim"> #include <stdio.h>
int
main (void)
{
int i;
while ((i = getchar()) != EOF)
{
unsigned char c = i;
printf ("read c = '%c'\n", c);
}
return 0;
}
</pre></pre>
<p class="noindent"><a name="index-signed-bitfield-option-697"></a><a name="index-unsigned-bitfield-option-698"></a><a name="index-bitfields_002c-portability-of-signed-vs-unsigned-699"></a><a name="index-g_t_0040option_007b_002dfunsigned_002dbitfields_007d-option-700"></a><a name="index-g_t_0040option_007b_002dfsigned_002dbitfields_007d-option-701"></a>The same considerations described in this section apply to the
definitions of bitfields in structs, which can be signed or unsigned by
default. In GCC, the default type of bitfields can be controlled using
the options <samp><span class="option">-fsigned-bitfields</span></samp> and <samp><span class="option">-funsigned-bitfields</span></samp>.
<div class="footnote">
<hr>
<h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> MacOS X (Darwin) on
PowerPC uses signed <code>char</code>, for consistency with other Darwin
architectures.</p>
<p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> There is also a subtle error even on
platforms with signed <code>char</code>—the <span class="sc">ascii</span> character 255
is spuriously interpreted as an end of file condition.</p>
<p class="footnote"><small>[<a name="fn-3" href="#fnd-3">3</a>]</small> If displayed, character code 255 often appears as <code>ÿ</code>.</p>
<hr></div>
</body></html>
|