C language Operators detailed introduction and sample code _c language

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

C operator

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. The C language has a rich operator built-in and provides the following types of operators:

    1. Arithmetic operators
    2. Relational operators
    3. logical operators
    4. Bitwise operators
    5. Assignment operator
    6. Miscellaneous operators

This chapter describes the arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators individually.

Arithmetic operators

The following table shows all the arithmetic operators supported by the C language. Assuming the value of variable A is 10, and the value of variable B is 20, then:

operator Description instance
+ Adds two operands A + B will get 30
- Subtract the second operand from the first operand A-b will get-10
* Multiply two operands A * B will get 200
/ Numerator divided by the denominator B/A will get 2
% Modulo operator, the remainder of the divide B% A will get 0
++ Self-increasing operator with integer value increased by 1 a++ will get 11
-- Self-subtraction operator, integer value reduced by 1 a--will get 9

Instance

Take a look at the example below to find out all the arithmetic operators available in the C language:

#include <stdio.h>

Main ()
{
 int a =;
 int B = Ten;
 int C;

 c = a + B;
 printf ("line 1-c value is%d\n", c);
 c = a-b;
 printf ("line 2-c value is%d\n", c);
 c = A * b;
 printf ("line 3-c value is%d\n", c);
 c = A/b;
 printf ("line 4-c value is%d\n", c);
 c = a% B;
 printf ("line 5-c value is%d\n", c);
 c = a++; 
 printf ("line 6-c value is%d\n", c);
 c = a--; 
 printf ("line 7-c value is%d\n", c);

}

When the above code is compiled and executed, it produces the following results:

Line 1-c is the value of line 2-c is line 3-c value is 210 line 4-c value is 2 line 5-c
The value is 1 line
6- The value of C is the value of line
7-c is 22

Relational operators

The following table shows all the relational operators supported by the C language. Assuming the value of variable A is 10, and the value of variable B is 20, then:

operator Description instance
== Checks whether the values of two operands are equal, and if they are equal, the condition is true. (A = = B) is not true.
!= Checks whether the values of two operands are equal, and if not, the condition is true. (A!= B) is true.
> Check that the value of the left operand is greater than the value of the right operand, and if so, the condition is true. (A > B) is not true.
< Check the value of the left operand to be less than the right operand, and if so, the condition is true. (A < B) is true.
>= Check that the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. (A >= B) is not true.
<= Check that the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. (A <= B) is true.

Instance

Take a look at the example below to find out all the relational operators available in the C language:

#include <stdio.h>

Main ()
{
 int a =;
 int B = Ten;
 int C;

 if (a = = b)
 {
  printf ("line 1-a equals b\n");
 }
 else
 {
  printf ("line 1-a not equal to b\n");
 }
 if (a < b)
 {
  printf ("line 2-a less than b\n");
 }
 else
 {
  printf ("line 2-a not less than b\n");
 }
 if (a > B)
 {
  printf ("line 3-a greater than b\n");
 }
 else
 {
  printf ("line 3-a is not more than b\n");
 }
 /* Change the value of a and B */
 a = 5;
 b =;
 if (a <= b)
 {
  printf ("line 4-a is less than or equal to b\n");
 }
 if (b >= a)
 {
  printf ("line 5-b is greater than or equal to b\n");
 }

When the above code is compiled and executed, it produces the following results:

Line 1-a Not equal to B line
2-a not less than B line
3-a greater than B line
4-a less than or equal to B line
5-b greater than or equal to B

logical operators

The following table shows all the relational logical operators supported by the C language. Assuming the value of variable A is 1, and the value of variable B is 0, then:

operator Description instance
&& Called Logic and operators. If all two operands are non-zero, the condition is true. (A && B) is false.
|| Called the logic or operator. If there are any nonzero zeros in the two operands, the condition is true. (A | | B) is true.
! is called a logical non operator. Used to reverse the logical state of the operand. If the condition is true, the logical non-operator makes it false. ! (A && B) is true.

Instance

Consider the following example to learn all the logical operators available in C:

#include <stdio.h>

Main ()
{
 int a = 5;
 int b =;
 int C;

 if (a && b)
 {
  printf ("Line 1-condition is true \ n")
 ;
 if (A | | b)
 {
  printf ("Line 2-condition is true \ n");
 }
 /* Change the value of a and B */
 a = 0;
 b = Ten;
 if (a && b)
 {
  printf ("Line 3-condition is true \ n")
 ;
 else
 {
  printf ("Line 3-condition is not true \ n");
 }
 if (!) ( A && b)
 {
  printf ("Line 4-condition is true \ n");
 }

When the above code is compiled and executed, it produces the following results:

Line 1-The condition is true line
2-The condition is true line
3-The condition is not true line
4-The condition is true

Bitwise operators

The bitwise operator acts on a bit and performs the operation bit-by-byte. &, | and ^ 's truth table looks like this:

P Q P & Q p | q p ^ Q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Suppose that if A = 60 and B = 13 are now represented in binary format, they are as follows:

A = 0011 1100
B = 0000 1101
-----------------
a&b = 0000 1100
a| B = 0011 1101
A^b = 0011 0001
~a = 1100 0011

The following table shows the bitwise operators supported by the C language. Assuming the value of variable A is 60, and the value of variable B is 13, then:

operator Description instance
& If the same exists in two operands, the binary and operator replicates one to the result. (A & B) will receive 12, that is 0000 1100
| If present in any operand, the binary OR operator replicates one to the result. (A | B) will receive 61, that is 0011 1101
^ If present in one of the operands but not in the same two operands, the binary XOR or operator replicates one to the result. (A ^ B) will be 49, that is 0011 0001
~ The binary complement operator is a unary operator with a "flip" bit effect. (~a) will get 61, that is, 1100 0011,2 complement form, signed binary number.
<< Binary left-shift operator. The value of the left operand shifts the number of digits specified by the right-hand operand. A << 2 will get 240, that is 1111 0000
>> Binary right shift operator. The value of the left operand shifts the number of digits specified by the right-hand operand to the right. A >> 2 will get 15, that is 0000 1111

Instance

Take a look at the example below to find out all the available bitwise operators in the C language:

#include <stdio.h>

Main ()
{

 unsigned int a =;	/* 0011 = 1100 * * 
 unsigned int b =;	/* = 0000 1101 *
 /int c = 0;   

 c = A & B;  /* = 0000 1100 
 /printf ("line 1-c value is%d\n", c);

 c = A | b;  /* 0011 = 1101 *
 /printf ("line 2-c value is%d\n", c);

 c = a ^ B;  /* 0011 = 0001
 /printf ("line 3-c value is%d\n", c);

 c = ~a;   /*-61 = 1100 0011
 /printf ("line 4-c value is%d\n", c);

 c = a << 2;  /* 1111 = 0000
 /printf ("line 5-c value is%d\n", c);

 c = a >> 2;  /* = 0000 1111 *
 /printf ("line 6-c value is%d\n", c);
}

When the above code is compiled and executed, it produces the following results:

Line 1-c value is the value of line 2-c is the value of line 3-c is line
4-c is -61 line
5-c
value is The 6-c value is 15.

Assignment operator

The following table lists the assignment operators supported by the C language:

operator Description instance
= A simple assignment operator that assigns the value of the right-hand operand to the left-hand operand c = A + B assigns the value of A + B to c
+= Plus and assignment operators, assigning the right-hand operand to the left-hand operand with the result of the left-hand operand C + + a equals c = C + A
-= Minus and assignment operators, assigning the left-hand operand minus the right-hand operand to the left-hand operand c-= A equals c = c-a
*= Multiply and assign operators, multiply the right-hand operand by the result of the left-hand operand to the left operand C *= a equals c = c * A
/= In addition to the assignment operator, assigns the left-hand operand to the left operand by dividing the result of the right-hand operand C/= A equals C = c/a
%= Modulo and assignment operators, asking for modulo assignment of two operands to left operand C%= a equals c = c% A
<<= Move left and assignment operator C <<= 2 equals c = C << 2
>>= Move right and assignment operator C >>= 2 equals c = C >> 2
&= Bitwise AND and assignment operators C &= 2 equals c = C & 2
^= Bitwise XOR and Assignment operators C ^= 2 equals c = c ^ 2
|= bitwise OR and assignment operators C |= 2 equals c = C | 2

Instance

See the example below for all the available assignment operators in the C language:

#include <stdio.h>

Main ()
{
 int a =;
 int C;

 c = A;
 printf ("Line 1-= operator instance, value of C =%d\n", c);

 C + A;
 printf ("Line 2-+ = operator instance, value of C =%d\n", c);

 c-= A;
 printf ("line 3-= operator instance, value of C =%d\n", c);

 c *= A;
 printf ("Line 4-*= operator instance, value of C =%d\n", c);

 c/= A;
 printf ("Line 5-/= operator instance, value of C =%d\n", c);

 c =;
 c%= A;
 printf ("line 6-%= operator instance, value of C =%d\n", c);

 C <<= 2;
 printf ("Line 7-<<= operator instance, value of C =%d\n", c);

 C >>= 2;
 printf ("Line 8->>= operator instance, value of C =%d\n", c);

 C &= 2;
 printf ("Line 9-&= operator instance, value of C =%d\n", c);

 C ^= 2;
 printf ("Line 10-^= operator instance, value of C =%d\n", c);

 C |= 2;
 printf ("line 11-|= operator instance, value of C =%d\n", c);

}

When the above code is compiled and executed, it produces the following results:

Line 1-= operator instance, the value of C = Line
2-+ = operator instance, the value of C = Line
3-= operator instance, C's value = Line
4-*= operator instance, C's value = 44 1 line
5-/= operator instance, C's value = line
6-%= operator instance, C's value = line
7-<<= operator instance, C's value = line
8-> >= operator instance, the value of C = one line
9-&= operator instance, the value of C = 2 line
10-^= operator instance, the value of c = 0 Line
11-|= operator instance, the value of C = 2

Miscellaneous Operators ↦sizeof & Ternary

The following table lists some of the other important operators supported by the C language, including sizeof and? :。

operator Description instance
sizeof () Returns the size of the variable. sizeof (a) returns 4, where A is an integer.
& Returns the address of a variable. &a; The actual address of the variable is given.
* Point to a variable. *a; Will point to a variable.
? : Conditional expression What if the condition is true? The value is X: Otherwise the value is Y

Instance

Consider the following example to find out all the miscellaneous operators available in the C language:

#include <stdio.h>

Main ()
{
 int a = 4;
 Short B;
 Double C;
 int* ptr;

 /* sizeof operator instance *
 /printf ("Line 1-variable a size =%d\n", sizeof (a));
 printf ("Line 2-variable b size =%d\n", sizeof (b));
 printf ("Line 3-variable c size =%d\n", sizeof (c));

 /* & and * operator instance * *
 ptr = &a;	/* ' PTR ' now contains the address of ' a '
 /printf ("A's value is%d\n", a);
 printf ("*ptr is%d\n", *ptr);

 /* Ternary operator instance
 /a = ten;
 b = (A = = 1)? 20:30;
 printf ("B's value is%d\n", b);

 b = (A = = 10)? 20:30;
 printf ("B's value is%d\n", b);
}

When the above code is compiled and executed, it produces the following results:

The value of a is 4
*ptr is 4 b The value is
20

Operator Precedence in C

The precedence of an operator determines the combination of items in an expression. This affects how an expression is evaluated. Some operators have higher precedence than other operators, for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.

For example x = 7 + 3 * 2, where x is assigned 13 instead of 20 because the operator * has a higher precedence than +, so first compute the multiplication 3*2 and then add 7.

The following table lists the operators by operator precedence from highest to lowest, with higher precedence operators appearing above the table, with lower precedence operators appearing below the table. In an expression, higher-priority operators are evaluated preferentially.

category operator combination of
Suffix () []->. ++ - - From left to right
One dollar + - ! ~ + +--(type) * & sizeof From right to left
Multiplication * / % From left to right
Add and Subtract + - From left to right
Shift << >> From left to right
Relationship < <= > >= From left to right
Equal == != From left to right
Bit and & From left to right
Bitwise exclusive OR XOR ^ From left to right
Bit OR OR | From left to right
Logic and && From left to right
Logical OR OR || From left to right
Conditions ?: From right to left
assigning values = + = = *=/=%=>>= <<= &= ^= |= From right to left
Comma , From left to right

Instance

Consider the following example to learn the precedence of operators in the C language:

#include <stdio.h>

Main ()
{
 int a =;
 int B = Ten;
 int c =;
 int d = 5;
 int e;
 
 E = (A + b) * C/D;  (*)/5
 printf ("(A + B) * C/D value is%d\n", e);

 E = ((A + b) * c)/D; (a)/5
 printf (((A + b) * C)/d is the value of%d\n ", e);

 E = (A + b) * (C/D); (a) * (15/5)
 printf ("(A + B) * (C/D) The value is%d\n", e);

 E = a + (b * c)/D;  (150/5)
 printf (the value of "A + (b * c)/d is%d\n", e);
 
 return 0;
}

When the above code is compiled and executed, it produces the following results:

(A + B) * The value of C/D (
(A + b) * C)/d is the value of (
A + b) * (C/D) is the value of
A + (b * c)/d is 50

The above is the C language operator of the basic data collation, follow-up continue to collate relevant information, thank you for your support of this site!

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.