1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Compatible Types (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Compatible Types (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Compatible Types (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="index.html" rel="up" title="Top">
|
---|
33 | <link href="Type-Conversions.html" rel="next" title="Type Conversions">
|
---|
34 | <link href="Old_002dStyle-Function-Definitions.html" rel="prev" title="Old-Style Function Definitions">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Compatible-Types"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Type-Conversions.html" accesskey="n" rel="next">Type Conversions</a>, Previous: <a href="Functions.html" accesskey="p" rel="prev">Functions</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Compatible-Types-1"></span><h2 class="chapter">23 Compatible Types</h2>
|
---|
64 | <span id="index-compatible-types"></span>
|
---|
65 | <span id="index-types_002c-compatible"></span>
|
---|
66 |
|
---|
67 | <p>Declaring a function or variable twice is valid in C only if the two
|
---|
68 | declarations specify <em>compatible</em> types. In addition, some
|
---|
69 | operations on pointers require operands to have compatible target
|
---|
70 | types.
|
---|
71 | </p>
|
---|
72 | <p>In C, two different primitive types are never compatible. Likewise for
|
---|
73 | the defined types <code>struct</code>, <code>union</code> and <code>enum</code>: two
|
---|
74 | separately defined types are incompatible unless they are defined
|
---|
75 | exactly the same way.
|
---|
76 | </p>
|
---|
77 | <p>However, there are a few cases where different types can be
|
---|
78 | compatible:
|
---|
79 | </p>
|
---|
80 | <ul>
|
---|
81 | <li> Every enumeration type is compatible with some integer type. In GNU
|
---|
82 | C, the choice of integer type depends on the largest enumeration
|
---|
83 | value.
|
---|
84 |
|
---|
85 |
|
---|
86 | </li><li> Array types are compatible if the element types are compatible
|
---|
87 | and the sizes (when specified) match.
|
---|
88 |
|
---|
89 | </li><li> Pointer types are compatible if the pointer target types are
|
---|
90 | compatible.
|
---|
91 |
|
---|
92 | </li><li> Function types that specify argument types are compatible if the
|
---|
93 | return types are compatible and the argument types are compatible,
|
---|
94 | argument by argument. In addition, they must all agree in whether
|
---|
95 | they use <code>...</code> to allow additional arguments.
|
---|
96 |
|
---|
97 | </li><li> Function types that don’t specify argument types are compatible if the
|
---|
98 | return types are.
|
---|
99 |
|
---|
100 | </li><li> Function types that specify the argument types are compatible with
|
---|
101 | function types that omit them, if the return types are compatible and
|
---|
102 | the specified argument types are unaltered by the argument promotions
|
---|
103 | (see <a href="Argument-Promotions.html">Argument Promotions</a>).
|
---|
104 | </li></ul>
|
---|
105 |
|
---|
106 | <p>In order for types to be compatible, they must agree in their type
|
---|
107 | qualifiers. Thus, <code>const int</code> and <code>int</code> are incompatible.
|
---|
108 | It follows that <code>const int *</code> and <code>int *</code> are incompatible
|
---|
109 | too (they are pointers to types that are not compatible).
|
---|
110 | </p>
|
---|
111 | <p>If two types are compatible ignoring the qualifiers, we call them
|
---|
112 | <em>nearly compatible</em>. (If they are array types, we ignore
|
---|
113 | qualifiers on the element types.<a id="DOCF7" href="#FOOT7"><sup>7</sup></a>)
|
---|
114 | Comparison of pointers is valid if the pointers’ target types are
|
---|
115 | nearly compatible. Likewise, the two branches of a conditional
|
---|
116 | expression may be pointers to nearly compatible target types.
|
---|
117 | </p>
|
---|
118 | <p>If two types are compatible ignoring the qualifiers, and the first
|
---|
119 | type has all the qualifiers of the second type, we say the first is
|
---|
120 | <em>upward compatible</em> with the second. Assignment of pointers
|
---|
121 | requires the assigned pointer’s target type to be upward compatible
|
---|
122 | with the right operand (the new value)’s target type.
|
---|
123 | </p>
|
---|
124 | <div class="footnote">
|
---|
125 | <hr>
|
---|
126 | <h4 class="footnotes-heading">Footnotes</h4>
|
---|
127 |
|
---|
128 | <h5><a id="FOOT7" href="#DOCF7">(7)</a></h3>
|
---|
129 | <p>This is a GNU C extension.</p>
|
---|
130 | </div>
|
---|
131 | <hr>
|
---|
132 | <div class="header">
|
---|
133 | <p>
|
---|
134 | Next: <a href="Type-Conversions.html" accesskey="n" rel="next">Type Conversions</a>, Previous: <a href="Functions.html" accesskey="p" rel="prev">Functions</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
135 | </div>
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | </body>
|
---|
140 | </html>
|
---|