C 연산자
연산자
Until C99, 50 operators. Since C11, 51 operators, _Alignof
. compound literal [C99] not included in the total.
- 6 bitwise operators:
- Unary bitwise NOT
~
. - Binary operators
<<
,>>
,&
,^
,|
.
- Unary bitwise NOT
- 5 binary operators:
- Bitwise left shift
<<
. - Bitwise right shift
>>
. - Bitwise AND
&
. - Bitwise exclusive or (XOR)
^
. - Bitwise inclusive or (OR)
|
.
- Bitwise left shift
- 3 logical operators:
- Logical AND
&&
. - Logical OR
||
. - Unary not
!
.
- Logical AND
- 11 assignment operators:
- Simple assignment
=
. - Compound assignment, augmented assignment, or combined assignment
*=
,/=
,%=
,+=
,-=
,<<=
,>>=
,&=
,^=
,|=
.
- Simple assignment
- 6 unary operators:
- Address of
&
. - Pointer type
*
. - Unary arithmetic
+
,-
,~
,!
.
- Address of
- 4 unary arithmetic operators:
- Plus (integer promotion)
+
, result is an arithmetic type. - Minus (additive inverse)
-
, result is an arithmetic type. - Bitwise NOT
~
, result is integer type. - Logical negation NOT
!
, result is a scalar type ofint
- Plus (integer promotion)
- 13 arithmetic operators:
- Unary
+
(+x
) and-
(-x
). - Additive
+
(x+y
) and-
(x-y
). - Multiplicative
*
(x*y
),/
(x/y
) and%
(x%y
). - Bitwise
~
(~x
),<<
(x<<y
),>>
(x>>y
),&
(x&y
),^
(x^y
) and|
(x|y
).
- Unary
- 3 multiplicative operators:
- Multiplication
*
. - Division
/
. - Modulo
%
, result is integer type.
- Multiplication
- 2 additive operators:
- Addition
+
. - Subtraction
-
.
- Addition
- 6 comparison operators:
- Equality
==
,!=
. - Relational
<
,>
,<=
,>=
.
- Equality
- 4 relational operators:
- Less than
<
. - Bigger than
>
. - Less than or equal to
<=
. - Bigger than or equal to
>=
.
- Less than
- 2 equality operators:
- Equal to
==
. - Not equal to
!=
.
- Equal to
- 5 member access:
- Array subscription
[]
(x[y]
). - Indirection operator, pointer type
*
(*x
). - Address operator
&
(&x
). - Access member of
struct
orunion
.
(x.y
). - Access member of
struct
orunion
using pointer->
(x->y
).
- Array subscription
- 2 prefix, increment and decrement operators:
++prefix
and--prefix
. - 2 postfix, increment and decrement operators:
postfix++
andpostfix--
. - 7 miscellaneous operators:
- Comma
,
. -
Conditional operator or ternary
? :
,x > y ? 1 : 0;
. - Function call
( )
,x(y,z)
. -
sizeof
, result is size in bytes. - [C11]
_Alignof(
type)
, result is the alignment requirement of the type. - Conversion, c-style cast:
(
type) x
. -
Compound literal[C99]
(
type){
initializer-list}
, the result is a unnamed object.
- Comma
Notes:
- Compound literal behave as an operator but is not explicit in the standard as part of the group operators, but in this guide is considered an operator.
- Same Symbol Different Operators. The behavior is defined by how it is used:
- Bitwise AND
&
vs Address Of&
. - Multiplication
*
vs Indirection Operator*
(Pointer). - Unary Minus
-
and Unary Plus+
vs Additive Operators-
and+
.
- Bitwise AND
- 💩 Applying comparison operators to Infinite or Not-a-Number (NaN) values will cause an exception. NaN does not compare to anything, even between NaN and NaN. To proper compare use
isnan
function from<math.h>
. To compare an Infinite value useisinf
orisfinite
macros. - ⚠️ Arithmetic operators applied to floating-point types may cause NaN or Infinite value.
연산자 우선 순위 및 연관성
- Associativity: Left-to-right
-
()
- Function call -
[]
- Array subscripting -
.
- Structure and union member access -
->
- Structure and union member access through pointer -
++
,--
- Suffix/postfix increment and decrement -
(type){list}
- Compound literal [C99]
-
- Associativity: Right-to-left
-
!
,~
- Logical NOT and bitwise NOT -
++
,--
- Prefix increment and decrement -
+
,-
- Unary plus and minus -
(type)
- Type cast -
sizeof
- Size-of -
*
- Indirection (dereference), pointer syntax -
&
- Address-of -
_Alignof
- Alignment requirement [C11]
-
- Associativity: Left-to-right
-
*
- Multiplication -
/
- Division -
%
- Modulus
-
- Associativity: Left-to-right
-
+
- Addition -
-
- Subtraction
-
- Associativity: Left-to-right
-
<<
- Bitwise left shift -
>>
- Bitwise right shift
-
- Associativity: Left-to-right
-
<
- Less than -
<=
- Less than OR equal to -
>
- Greater than -
>=
- Greater than OR equal to
-
- Associativity: Left-to-right
-
==
- Equal to -
!=
- NOT equal to
-
- Associativity: Left-to-right
-
&
- Bitwise AND
-
- Associativity: Left-to-right
-
^
- Bitwise XOR (exclusive or)
-
- Associativity: Left-to-right
-
|
- Bitwise OR (inclusive or)
-
- Associativity: Left-to-right
-
&&
- Logical AND
-
- Associativity: Left-to-right
-
||
- Logical OR
-
- Associativity: Right-to-Left
-
?:
- Ternary conditional. Test your compiler precedence on this one.
-
- Associativity: Right-to-Left
-
=
- Simple assignment -
+=
,-=
- Assignment by sum and difference -
*=
,/=
,%=
- Assignment by product, quotient, and remainder -
<<=
,>>=
- Assignment by bitwise left shift and right shift -
&=
,^=
,|=
- Assignment by bitwise AND, XOR, and OR
-
- Associativity: Left-to-right
-
,
- Comma
-
Notes:
- Precedence and associativity are independent from order of evaluation.
- C language standard doesn't specify operator precedence.
- When parsing an expression, an operator which is listed on the same row will be bound tighter
(
as if by parentheses)
to its arguments than any operator that is listed on a row further below it. For example, the expression*p++
is parsed as*(p++)
, and not as(*p)++
. - Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression
a=b=c
is parsed asa=(b=c)
, and not as(a=b)=c
because of right-to-left associativity.
피연산자를 변경하는 연산자
Most operators does not change the operands values, but a few do:
- Assignment operators change the left-operand value.
- Increment and decrement operators change the operands value.
This is why some programmers use constant value on the left operand. E.g.: instead of ==
for equality comparison, the programmer mistakable use =
that is the simple assignment, the compiler will not be able to recognize the problem:
if(value = 0){} // 💩, It is valid expression for the compiler
To better mitigate risks, exchange the position and the compiler will be able to output a proper error if this mistake happen.
if(0 = value){} // Compiler output a proper error on this
연산자의 대체 표현
<iso646.h>
define 11 macros, that are alternative spellings for some operators. Why <iso646.h>
? European keyboards are not programming-friendly. Some programmers argue that this make easy to parse by human eyes when heavy use of bitwise operators is done with comparison operators.
Symbol | Alternative spelling | Example |
---|---|---|
&& |
and |
x and y |
&= |
and_eq |
x and_eq y |
& |
bitand |
x bitand y |
| |
bitor |
x bitor |
~ |
compl |
x compl y |
! |
not |
not x |
!= |
not_eq |
not_eq x |
|| |
or |
x or y |
|= |
or_eq |
x or_eq y |
^ |
xor |
x xor y |
^= |
xor_eq |
x xor_eq y |
References
Reference
이 문제에 관하여(C 연산자), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/bkddev/c-operators-g7g텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)