source: public/doc/gnu-c/Compatible-Types.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.1 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
68declarations specify <em>compatible</em> types. In addition, some
69operations on pointers require operands to have compatible target
70types.
71</p>
72<p>In C, two different primitive types are never compatible. Likewise for
73the defined types <code>struct</code>, <code>union</code> and <code>enum</code>: two
74separately defined types are incompatible unless they are defined
75exactly the same way.
76</p>
77<p>However, there are a few cases where different types can be
78compatible:
79</p>
80<ul>
81<li> Every enumeration type is compatible with some integer type. In GNU
82C, the choice of integer type depends on the largest enumeration
83value.
84
85
86</li><li> Array types are compatible if the element types are compatible
87and the sizes (when specified) match.
88
89</li><li> Pointer types are compatible if the pointer target types are
90compatible.
91
92</li><li> Function types that specify argument types are compatible if the
93return types are compatible and the argument types are compatible,
94argument by argument. In addition, they must all agree in whether
95they use <code>...</code> to allow additional arguments.
96
97</li><li> Function types that don&rsquo;t specify argument types are compatible if the
98return types are.
99
100</li><li> Function types that specify the argument types are compatible with
101function types that omit them, if the return types are compatible and
102the 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
107qualifiers. Thus, <code>const int</code> and <code>int</code> are incompatible.
108It follows that <code>const int *</code> and <code>int *</code> are incompatible
109too (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
113qualifiers on the element types.<a id="DOCF7" href="#FOOT7"><sup>7</sup></a>)
114Comparison of pointers is valid if the pointers&rsquo; target types are
115nearly compatible. Likewise, the two branches of a conditional
116expression may be pointers to nearly compatible target types.
117</p>
118<p>If two types are compatible ignoring the qualifiers, and the first
119type has all the qualifiers of the second type, we say the first is
120<em>upward compatible</em> with the second. Assignment of pointers
121requires the assigned pointer&rsquo;s target type to be upward compatible
122with the right operand (the new value)&rsquo;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>
134Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.