C Language Relational Operator example detailed _c language

Source: Internet
Author: User

It is often necessary to compare the size of two data in a program to determine the next step in the program. For example, a program limits the use of only adults, children because of age, not the right to use. At this time the program needs to get the user input age and make a judgment, if more than 18 years old normal operation, otherwise give no right to use the hint.

Operators that compare two data sizes are called relational operators (relational Operators).

The following relational operators are available in the C language:

1) < (less than)
2) <= (less than or equal to)
3) > (greater than)
4) >= (greater than or equal to)
5) = = = (equal to)
6)!= (not equal to)

Relational operators are both binocular operators, and their binding is left-bound. The precedence of the relational operator is lower than the arithmetic operator, which is higher than the assignment operator. The precedence of,<, <=, >, >= is the same in six relational operators, which is higher than = = and!=,== and!=.

In the C language, some operators have two operands, such as 10+20,10 and 20 are operands, + is an operator. We call such an operator a binocular operator. Similarly, the operator with one operand is called the monocular operator, and the operator with three operands is called the triple-head operator.

Common binocular operators have + 、-、 *,/etc., the monocular operator has + + 、--, and so on, the three-mesh operator is only one, is? : We will describe it in more detail in the C language conditional operator.

The two sides of a relational operator can be variables, data, or expressions, such as:

1) a+b>c-d
2) X>3/2
3) ' A ' +1<c
4)-i-5*j==k+1

Relational operators can also be nested, for example:

1) a> (b>c)
2) a!= (C==d)

The result of the relational operator is only 0 or 1. When the condition is set up, the result is 1, the condition is not tenable and the result is 0. For example:

5>0 was established with a value of 1;
34-12>100 is not tenable, its value is 0;
(a=3) > (b=5) because 3>5 is not tenable, so its value is 0.

We call the result of the operation 1 "true", which means that the condition is set up, and 0 is called "false", which means that the condition is not tenable.

Let's look at the result output of the relational operator:

#include <stdio.h>
int main () {
 char c= ' k ';
 int I=1, j=2, k=3;
 float x=3e+5, y=0.85;
 int result_1 = ' A ' +5<c, result_2 = x-5.25<=x+y;
 printf ("%d,%d\n", result_1,-i-2*j>=k+1);
 printf ("%d,%d\n", 1<j<5, result_2);
 printf ("%d,%d\n", I+j+k==-2*j, k==j==i+5);
 return 0;
}

Run Result:

1, 0
1, 1
0, 0

For expressions with multiple relational operators, such as k==j==i+5, according to the left binding of operators, first compute K==J, the formula is not tenable, its value is 0, then calculate 0==i+5, also does not set up, so the expression value is 0.

The above is the introduction of the relational operators, hoping to help students to learn C language.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.