Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Comparison operators

      From cppreference.com
      <c‎ |language
       
       
       
       

      Comparison operators are binary operators that test a condition and return1 if that condition is logicallytrue and0 if that condition isfalse.

      Operator Operator name Example Description
      == equal toa== ba is equal tob
      != not equal toa!= ba is not equal tob
      < less thana< ba is less thanb
      > greater thana> ba is greater thanb
      <= less than or equal toa<= ba is less than or equal tob
      >= greater than or equal toa>= ba is greater than or equal tob

      Contents

      [edit]Relational operators

      The relational operator expressions have the form

      lhs<rhs (1)
      lhs>rhs (2)
      lhs<=rhs (3)
      lhs>=rhs (4)
      1) less-than expression
      2) greater-than expression
      3) less or equal expression
      4) greater or equal expression

      where

      lhs,rhs - expressions that both have real type or both have pointer to object type

      The type of any relational operator expression isint, and its value (which is not an lvalue) is1 when the specified relationship holds true and0 when the specified relationship does not hold.

      Iflhs andrhs are expressions of anyreal type, then

      • usual arithmetic conversions are performed
      • the values of the operands after conversion are compared in the usual mathematical sense (except that positive and negative zeroes compare equal and any comparison involving a NaN value returns zero)

      Note that complex and imaginary numbers cannot be compared with these operators.

      Iflhs andrhs are expressions of pointer type, they must be both pointers to objects ofcompatible types, except that qualifications of the pointed-to objects are ignored.

      • a pointer to an object that is not an element of an array is treated as if it were pointing to an element of an array with one element
      • if two pointers point to the same object, or both point one past the end of the same array, they compare equal
      • if two pointers point to different elements of the same array, the one pointing at the element with the larger index compares greater.
      • if one pointer points to the element of an array and the other pointer points one past the end of the same array, the one-past-the-end pointer compares greater
      • if the two pointers point to members of the samestruct, the pointer to the member declared later in the struct definition compares greater than then pointer to the member declared earlier.
      • pointers to members of the same union compare equal
      • all other pointer comparisons invoke undefined behavior
      Run this code
      #include <assert.h>int main(void){assert(1<2);assert(2+2<=4.0);// int converts to double, two 4.0's compare equal struct{int x,y;} s;assert(&s.x<&s.y);// struct members compare in order of declaration double d=0.0/0.0;// NaNassert(!(d< d));assert(!(d> d));assert(!(d<= d));assert(!(d>= d));assert(!(d== d)); float f=0.1;// f = 0.100000001490116119384765625double g=0.1;// g = 0.1000000000000000055511151231257827021181583404541015625assert(f> g);// different values}

      [edit]Equality operators

      The equality operator expressions have the form

      lhs==rhs (1)
      lhs!=rhs (2)
      1) equal-to expression
      2) not equal to expression

      where

      lhs,rhs - expressions that
      (since C23)
      • both are pointers to objects or functions ofcompatible types, ignoring qualifiers of the pointed-to types
      • one is a pointer to object and the other is a pointer to (possibly qualified)void
      • one is a pointer to object or function and the other is a null pointer constant such asNULLornullptr(since C23)

      The type of any equality operator expression isint, and its value (which is not an lvalue) is1 when the specified relationship holds true and0 when the specified relationship does not hold.

      • if both operands have arithmetic types,usual arithmetic conversions are performed and the resulting values are compared in the usual mathematical sense (except that positive and negative zeroes compare equal and any comparison involving a NaN value, including equality with itself, returns zero). In particular, values of complex type are equal if their real parts compare equal and their imaginary parts compare equal.
      (since C23)
      • if one operand is a pointer and the other is a null pointer constant, the null pointer constant is firstconverted to the type of the pointer (which gives a null pointer value), and the two pointers are compared as described below
      • if one operand is a pointer and the other is a pointer to void, the non-void pointer isconverted to the pointer to void and the two pointers are compared as described below
      • two pointers compare equal if any of the following is true:
      • they are both null pointer values of their type
      • they are both pointers to the same object or function
      • one pointer is to a struct/union/array object and the other is to its first member/any member/first element
      • they are both pointing one past the last element of the same array
      • one is one past the end of an array, and the other is at the start of a different array (of the same type) that follows the first in a larger array or in a struct with no padding

      (as with relational operators, pointers to objects that aren't elements of any array behave as pointers to elements of arrays of size 1)

      [edit]Notes

      Objects of struct type do not compare equal automatically, and comparing them withmemcmp is not reliable because the padding bytes may have any values.

      Because pointer comparison works with pointers to void, the macroNULL may be defined as(void*)0 in C, although that would be invalid in C++ where void pointers do not implicitly convert to typed pointers

      Care must be taken when comparing floating-point values for equality, because the results of many operations cannot be represented exactly and must be rounded. In practice, floating-point numbers are usually compared allowing for the difference of one or more units of the last place.

      Run this code
      #include <assert.h>int main(void){assert(2+2==4.0);// int converts to double, two 4.0's compare equal int n[2][3]={1,2,3,4,5,6};int* p1=&n[0][2];// last element in the first rowint* p2=&n[1][0];// start of second rowassert(p1+1== p2);// compare equal double d=0.0/0.0;// NaNassert( d!= d);// NaN does not equal itself float f=0.1;// f = 0.100000001490116119384765625double g=0.1;// g = 0.1000000000000000055511151231257827021181583404541015625assert(f!= g);// different values}

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.8 Relational operators (p: 68-69)
      • 6.5.9 Equality operators (p: 69-70)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.8 Relational operators (p: 95-96)
      • 6.5.9 Equality operators (p: 96-97)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.8 Relational operators (p: 85-86)
      • 6.5.9 Equality operators (p: 86-87)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.8 Relational operators
      • 3.3.9 Equality operators

      [edit]See also

      Operator precedence

      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      a= b
      a+= b
      a-= b
      a*= b
      a/= b
      a%= b
      a&= b
      a|= b
      a^= b
      a<<= b
      a>>= b

      ++a
      --a
      a++
      a--

      +a
      -a
      a+ b
      a- b
      a* b
      a/ b
      a% b
      ~a
      a& b
      a| b
      a^ b
      a<< b
      a>> b

      !a
      a&& b
      a|| b

      a== b
      a!= b
      a< b
      a> b
      a<= b
      a>= b

      a[b]
      *a
      &a
      a->b
      a.b

      a(...)
      a, b
      (type) a
      a? b: c
      sizeof


      _Alignof
      (since C11)
      (until C23)

      alignof
      (since C23)

      C++ documentation forComparison operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/operator_comparison&oldid=152706"

      [8]ページ先頭

      ©2009-2026 Movatter.jp