C-Operators.html 9.9 KB

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