You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

C-Operators.html 9.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>C Operators (Debugging with GDB)</title>
  17. <meta name="description" content="C Operators (Debugging with GDB)">
  18. <meta name="keywords" content="C Operators (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="C.html#C" rel="up" title="C">
  26. <link href="C-Constants.html#C-Constants" rel="next" title="C Constants">
  27. <link href="C.html#C" rel="prev" title="C">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style: oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="C-Operators"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="C-Constants.html#C-Constants" accesskey="n" rel="next">C Constants</a>, Up: <a href="C.html#C" accesskey="u" rel="up">C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="C-and-C_002b_002b-Operators"></a>
  64. <h4 class="subsubsection">15.4.1.1 C and C<tt>++</tt> Operators</h4>
  65. <a name="index-C-and-C_002b_002b-operators"></a>
  66. <p>Operators must be defined on values of specific types. For instance,
  67. <code>+</code> is defined on numbers, but not on structures. Operators are
  68. often defined on groups of types.
  69. </p>
  70. <p>For the purposes of C and C<tt>++</tt>, the following definitions hold:
  71. </p>
  72. <ul>
  73. <li> <em>Integral types</em> include <code>int</code> with any of its storage-class
  74. specifiers; <code>char</code>; <code>enum</code>; and, for C<tt>++</tt>, <code>bool</code>.
  75. </li><li> <em>Floating-point types</em> include <code>float</code>, <code>double</code>, and
  76. <code>long double</code> (if supported by the target platform).
  77. </li><li> <em>Pointer types</em> include all types defined as <code>(<var>type</var> *)</code>.
  78. </li><li> <em>Scalar types</em> include all of the above.
  79. </li></ul>
  80. <p>The following operators are supported. They are listed here
  81. in order of increasing precedence:
  82. </p>
  83. <dl compact="compact">
  84. <dt><code>,</code></dt>
  85. <dd><p>The comma or sequencing operator. Expressions in a comma-separated list
  86. are evaluated from left to right, with the result of the entire
  87. expression being the last expression evaluated.
  88. </p>
  89. </dd>
  90. <dt><code>=</code></dt>
  91. <dd><p>Assignment. The value of an assignment expression is the value
  92. assigned. Defined on scalar types.
  93. </p>
  94. </dd>
  95. <dt><code><var>op</var>=</code></dt>
  96. <dd><p>Used in an expression of the form <code><var>a</var>&nbsp;<var>op</var>=&nbsp;<var>b</var></code><!-- /@w -->,
  97. and translated to <code><var>a</var>&nbsp;=&nbsp;<var>a&nbsp;op&nbsp;b</var></code><!-- /@w -->.
  98. <code><var>op</var>=</code><!-- /@w --> and <code>=</code> have the same precedence. The operator
  99. <var>op</var> is any one of the operators <code>|</code>, <code>^</code>, <code>&amp;</code>,
  100. <code>&lt;&lt;</code>, <code>&gt;&gt;</code>, <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>.
  101. </p>
  102. </dd>
  103. <dt><code>?:</code></dt>
  104. <dd><p>The ternary operator. <code><var>a</var> ? <var>b</var> : <var>c</var></code> can be thought
  105. of as: if <var>a</var> then <var>b</var> else <var>c</var>. The argument <var>a</var>
  106. should be of an integral type.
  107. </p>
  108. </dd>
  109. <dt><code>||</code></dt>
  110. <dd><p>Logical <small>OR</small>. Defined on integral types.
  111. </p>
  112. </dd>
  113. <dt><code>&amp;&amp;</code></dt>
  114. <dd><p>Logical <small>AND</small>. Defined on integral types.
  115. </p>
  116. </dd>
  117. <dt><code>|</code></dt>
  118. <dd><p>Bitwise <small>OR</small>. Defined on integral types.
  119. </p>
  120. </dd>
  121. <dt><code>^</code></dt>
  122. <dd><p>Bitwise exclusive-<small>OR</small>. Defined on integral types.
  123. </p>
  124. </dd>
  125. <dt><code>&amp;</code></dt>
  126. <dd><p>Bitwise <small>AND</small>. Defined on integral types.
  127. </p>
  128. </dd>
  129. <dt><code>==<span class="roman">, </span>!=</code></dt>
  130. <dd><p>Equality and inequality. Defined on scalar types. The value of these
  131. expressions is 0 for false and non-zero for true.
  132. </p>
  133. </dd>
  134. <dt><code>&lt;<span class="roman">, </span>&gt;<span class="roman">, </span>&lt;=<span class="roman">, </span>&gt;=</code></dt>
  135. <dd><p>Less than, greater than, less than or equal, greater than or equal.
  136. Defined on scalar types. The value of these expressions is 0 for false
  137. and non-zero for true.
  138. </p>
  139. </dd>
  140. <dt><code>&lt;&lt;<span class="roman">, </span>&gt;&gt;</code></dt>
  141. <dd><p>left shift, and right shift. Defined on integral types.
  142. </p>
  143. </dd>
  144. <dt><code>@</code></dt>
  145. <dd><p>The <small>GDB</small> &ldquo;artificial array&rdquo; operator (see <a href="Expressions.html#Expressions">Expressions</a>).
  146. </p>
  147. </dd>
  148. <dt><code>+<span class="roman">, </span>-</code></dt>
  149. <dd><p>Addition and subtraction. Defined on integral types, floating-point types and
  150. pointer types.
  151. </p>
  152. </dd>
  153. <dt><code>*<span class="roman">, </span>/<span class="roman">, </span>%</code></dt>
  154. <dd><p>Multiplication, division, and modulus. Multiplication and division are
  155. defined on integral and floating-point types. Modulus is defined on
  156. integral types.
  157. </p>
  158. </dd>
  159. <dt><code>++<span class="roman">, </span>--</code></dt>
  160. <dd><p>Increment and decrement. When appearing before a variable, the
  161. operation is performed before the variable is used in an expression;
  162. when appearing after it, the variable&rsquo;s value is used before the
  163. operation takes place.
  164. </p>
  165. </dd>
  166. <dt><code>*</code></dt>
  167. <dd><p>Pointer dereferencing. Defined on pointer types. Same precedence as
  168. <code>++</code>.
  169. </p>
  170. </dd>
  171. <dt><code>&amp;</code></dt>
  172. <dd><p>Address operator. Defined on variables. Same precedence as <code>++</code>.
  173. </p>
  174. <p>For debugging C<tt>++</tt>, <small>GDB</small> implements a use of &lsquo;<samp>&amp;</samp>&rsquo; beyond what is
  175. allowed in the C<tt>++</tt> language itself: you can use &lsquo;<samp>&amp;(&amp;<var>ref</var>)</samp>&rsquo;
  176. to examine the address
  177. where a C<tt>++</tt> reference variable (declared with &lsquo;<samp>&amp;<var>ref</var></samp>&rsquo;) is
  178. stored.
  179. </p>
  180. </dd>
  181. <dt><code>-</code></dt>
  182. <dd><p>Negative. Defined on integral and floating-point types. Same
  183. precedence as <code>++</code>.
  184. </p>
  185. </dd>
  186. <dt><code>!</code></dt>
  187. <dd><p>Logical negation. Defined on integral types. Same precedence as
  188. <code>++</code>.
  189. </p>
  190. </dd>
  191. <dt><code>~</code></dt>
  192. <dd><p>Bitwise complement operator. Defined on integral types. Same precedence as
  193. <code>++</code>.
  194. </p>
  195. </dd>
  196. <dt><code>.<span class="roman">, </span>-&gt;</code></dt>
  197. <dd><p>Structure member, and pointer-to-structure member. For convenience,
  198. <small>GDB</small> regards the two as equivalent, choosing whether to dereference a
  199. pointer based on the stored type information.
  200. Defined on <code>struct</code> and <code>union</code> data.
  201. </p>
  202. </dd>
  203. <dt><code>.*<span class="roman">, </span>-&gt;*</code></dt>
  204. <dd><p>Dereferences of pointers to members.
  205. </p>
  206. </dd>
  207. <dt><code>[]</code></dt>
  208. <dd><p>Array indexing. <code><var>a</var>[<var>i</var>]</code> is defined as
  209. <code>*(<var>a</var>+<var>i</var>)</code>. Same precedence as <code>-&gt;</code>.
  210. </p>
  211. </dd>
  212. <dt><code>()</code></dt>
  213. <dd><p>Function parameter list. Same precedence as <code>-&gt;</code>.
  214. </p>
  215. </dd>
  216. <dt><code>::</code></dt>
  217. <dd><p>C<tt>++</tt> scope resolution operator. Defined on <code>struct</code>, <code>union</code>,
  218. and <code>class</code> types.
  219. </p>
  220. </dd>
  221. <dt><code>::</code></dt>
  222. <dd><p>Doubled colons also represent the <small>GDB</small> scope operator
  223. (see <a href="Expressions.html#Expressions">Expressions</a>). Same precedence as <code>::</code>,
  224. above.
  225. </p></dd>
  226. </dl>
  227. <p>If an operator is redefined in the user code, <small>GDB</small> usually
  228. attempts to invoke the redefined version instead of using the operator&rsquo;s
  229. predefined meaning.
  230. </p>
  231. <hr>
  232. <div class="header">
  233. <p>
  234. Next: <a href="C-Constants.html#C-Constants" accesskey="n" rel="next">C Constants</a>, Up: <a href="C.html#C" accesskey="u" rel="up">C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  235. </div>
  236. </body>
  237. </html>