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>Assignment Type Conversions (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Assignment Type Conversions (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Assignment Type Conversions (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="Type-Conversions.html" rel="up" title="Type Conversions">
|
---|
33 | <link href="Argument-Promotions.html" rel="next" title="Argument Promotions">
|
---|
34 | <link href="Explicit-Type-Conversion.html" rel="prev" title="Explicit Type Conversion">
|
---|
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="Assignment-Type-Conversions"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Argument-Promotions.html" accesskey="n" rel="next">Argument Promotions</a>, Previous: <a href="Explicit-Type-Conversion.html" accesskey="p" rel="prev">Explicit Type Conversion</a>, Up: <a href="Type-Conversions.html" accesskey="u" rel="up">Type Conversions</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="Assignment-Type-Conversions-1"></span><h3 class="section">24.2 Assignment Type Conversions</h3>
|
---|
64 | <span id="index-assignment-type-conversions"></span>
|
---|
65 |
|
---|
66 | <p>Certain type conversions occur automatically in assignments
|
---|
67 | and certain other contexts. These are the conversions
|
---|
68 | assignments can do:
|
---|
69 | </p>
|
---|
70 | <ul>
|
---|
71 | <li> Converting any numeric type to any other numeric type.
|
---|
72 |
|
---|
73 | </li><li> Converting <code>void *</code> to any other pointer type
|
---|
74 | (except pointer-to-function types).
|
---|
75 |
|
---|
76 | </li><li> Converting any other pointer type to <code>void *</code>.
|
---|
77 | (except pointer-to-function types).
|
---|
78 |
|
---|
79 | </li><li> Converting 0 (a null pointer constant) to any pointer type.
|
---|
80 |
|
---|
81 | </li><li> Converting any pointer type to <code>bool</code>. (The result is
|
---|
82 | 1 if the pointer is not null.)
|
---|
83 |
|
---|
84 | </li><li> Converting between pointer types when the left-hand target type is
|
---|
85 | upward compatible with the right-hand target type. See <a href="Compatible-Types.html">Compatible Types</a>.
|
---|
86 | </li></ul>
|
---|
87 |
|
---|
88 | <p>These type conversions occur automatically in certain contexts,
|
---|
89 | which are:
|
---|
90 | </p>
|
---|
91 | <ul>
|
---|
92 | <li> An assignment converts the type of the right-hand expression
|
---|
93 | to the type wanted by the left-hand expression. For example,
|
---|
94 |
|
---|
95 | <div class="example">
|
---|
96 | <pre class="example">double i;
|
---|
97 | i = 5;
|
---|
98 | </pre></div>
|
---|
99 |
|
---|
100 | <p>converts 5 to <code>double</code>.
|
---|
101 | </p>
|
---|
102 | </li><li> A function call, when the function specifies the type for that
|
---|
103 | argument, converts the argument value to that type. For example,
|
---|
104 |
|
---|
105 | <div class="example">
|
---|
106 | <pre class="example">void foo (double);
|
---|
107 | foo (5);
|
---|
108 | </pre></div>
|
---|
109 |
|
---|
110 | <p>converts 5 to <code>double</code>.
|
---|
111 | </p>
|
---|
112 | </li><li> A <code>return</code> statement converts the specified value to the type
|
---|
113 | that the function is declared to return. For example,
|
---|
114 |
|
---|
115 | <div class="example">
|
---|
116 | <pre class="example">double
|
---|
117 | foo ()
|
---|
118 | {
|
---|
119 | return 5;
|
---|
120 | }
|
---|
121 | </pre></div>
|
---|
122 |
|
---|
123 | <p>also converts 5 to <code>double</code>.
|
---|
124 | </p></li></ul>
|
---|
125 |
|
---|
126 | <p>In all three contexts, if the conversion is impossible, that
|
---|
127 | constitutes an error.
|
---|
128 | </p>
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 | </body>
|
---|
133 | </html>
|
---|