The usage of colon (:) in C language includes: used to mark statements; as conditional operator; specify range; access structure members; define bit fields; perform type conversion; used for macro definition.
Usage of: in C language
The colon (:) in C language is a multi-purpose operator , has the following usage:
1. Label statement
A colon can be used to mark a statement, such as the beginning of a loop or conditional statement. Marker statements can be used in jump or goto statements.
<code class="c">my_label: // 代码块</code>
2. Conditional operator
The colon is the second part of the conditional operator, which will return one of two values based on the value of the conditional expression.
<code class="c">条件 ? 值1 : 值2</code>
3. Range operator
The colon can be used to specify a range, such as a subset of an array or a string.
<code class="c">数组[起始索引 : 结束索引 + 1]</code>
4. Structure member access
The colon can be used to access the members of the structure.
<code class="c">结构体名称.成员名称</code>
5. Bit field
The colon can be used to specify the width of the bit field.
<code class="c">struct { unsigned int my_bitfield : 4; } my_struct;</code>
6. Type conversion
The colon can be used to explicitly convert the type of an expression to another type.
<code class="c">(类型) 表达式</code>
7. Macro definition
A colon can be used to define a macro, where the colon separates the macro name from the macro expansion content.
<code class="c">#define 宏名称 : 宏展开内容</code>
The above is the detailed content of Usage of: in c language. For more information, please follow other related articles on the PHP Chinese website!