天天看點

c語言中指派運算符優先級_C / C ++中的指派運算符

c語言中指派運算符優先級

Assignment operators are used to assign the value/result of the expression to a variable (constant – in case of constant declaration). While executing an assignment operator based statement, it assigns the value (or the result of the expression) which is written at the right side to the variable which is written on the left side.

指派運算符用于将表達式的值/結果賦給變量(常量-在常量聲明的情況下)。 在執行基于指派運算符的語句時,它将在右側寫入的值(或表達式的結果)配置設定給在左側寫入的變量。

Syntax: 句法:
variable = value;
           

指派運算符的類型 (Type of the assignment operators)

C/C++ language provides a simple assignment operator that is "=", but some of the other assignment operators (which are the combination of assignment and other operators) can be used.

C / C ++語言提供了一個簡單的指派運算符 “ =” ,但是可以使用其他一些指派運算符 (它們是指派運算符和其他運算符的組合)。

The assignment operators are,

指派運算符是

SNo. Operator Description Example
1 = Simple assignment operator x = 10
2 += Add and assignment operator x += 10
3 -= Subtract and assignment operator x -=10
4 *= Multiply and assignment operator x *=10
5 /= Divide and assignment operator x /=10
6 %= Modules and assignment operator x %=10
7 <<= Left shift and assignment operator x <<= 10
8 >>= Right shift and assignment operator x =>>10
9 &= Bitwise AND and assignment operator x &= 10
10 |= Bitwise OR and assignment operator x |= 10
11 ^| Bitwise XOR and assignment operator x ^= 10
編号 操作員 描述
1個 = 簡單的指派運算符 x = 10
2 + = 添加和指派運算符 x + = 10
3 -= 減法和指派運算符 x-= 10
4 * = 乘法和指派運算符 x * = 10
5 / = 除法和指派運算符 x / = 10
6 %= 子產品和指派運算符 x%= 10
7 << = 左移和指派運算符 x << = 10
8 >> = 右移和指派運算符 x = >> 10
9 &= 按位與和指派運算符 x&= 10
10 | = 按位或與指派運算符 x | = 10
11 ^ | 按位XOR和指派運算符 x ^ = 10

Note: On the right side, a value, expression, or any variable can be used.

注意:在右側,可以使用值,表達式或任何變量。

1)簡單指派運算符(=) (1) Simple assignment operator (=))

It is a simple assignment operator which is used to assign the value and the result of the expression to the variable.

它是一個簡單的指派運算符,用于将值和表達式的結果指派給變量。

Syntax: 句法:
variable = value;
           
Example: 例:
// C++ program to demonstrate the
// example of = operator

#include <iostream>
using namespace std;

int main()
{
    int x = 0;

    x = 10;
    cout << "value of x = " << x << endl;

    return 0;
}
           
Output: 輸出:
value of x = 10
           

2)加和指派運算符(+ =) (2) Add and assignment operator (+=))

It adds the value or result of the expression to the current value of the variable and assigns the result to the variable.

它将表達式的值或結果添加到變量的目前值,并将結果配置設定給變量。

Syntax: 句法:
variable += value;
         equivalent to:                 variable = variable + value;
           
Example: 例:
// C++ program to demonstrate the
// example of += operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;

    cout << "Before the operation, x = " << x << endl;
    x += 5;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 15
           

3)減法和指派運算符(-=) (3) Subtract and assignment operator (-=))

It subtracts the value or result of the expression to the current value of the variable and assigns the result to the variable.

它将表達式的值或結果減去該變量的目前值,并将結果配置設定給該變量。

Syntax: 句法:
variable -= value;
         equivalent to:                 variable = variable - value;
           
Example: 例:
// C++ program to demonstrate the
// example of -= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;

    cout << "Before the operation, x = " << x << endl;
    x -= 5;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 5
           

4)乘法和指派運算符(* =) (4) Multiply and assignment operator (*=))

It multiplies the value or result of the expression to the current value of the variable and assigns the result to the variable.

它将表達式的值或結果與變量的目前值相乘,并将結果配置設定給變量。

Syntax: 句法:
variable *= value;
         equivalent to:                 variable = variable * value;
           
Example: 例:
// C++ program to demonstrate the
// example of *= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;

    cout << "Before the operation, x = " << x << endl;
    x *= 5;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 50
           

5)除法和指派運算符(/ =) (5) Divide and assignment operator (/=))

It divides the value or result of the expression with the current value of the variable and assigns the result (quotient) to the variable.

它将表達式的值或結果除以變量的目前值,然後将結果(商)配置設定給變量。

Syntax: 句法:
variable /= value;
         equivalent to:                 variable = variable / value;
           
Example: 例:
// C++ program to demonstrate the
// example of /= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;

    cout << "Before the operation, x = " << x << endl;
    x /= 5;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 2
           

6)子產品和指派運算符(%=) (6) Modules and assignment operator (%=))

It divides the value or result of the expression with the current value of the variable and assigns the result (remainder) to the variable.

它将表達式的值或結果除以變量的目前值,然後将結果(餘數)配置設定給變量。

Syntax: 句法:
variable %= value;
         equivalent to:                 variable = variable % value;
           
Example: 例:
// C++ program to demonstrate the
// example of %= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;

    cout << "Before the operation, x = " << x << endl;
    x %= 5;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 0
           

7)左移和指派運算符(<< =) (7) Left shift and assignment operator (<<=))

It shifts the value of the variable by given number of bits (value) to the left and assigns the result to the variable.

它将變量的值向左移動給定的位數(值),并将結果配置設定給變量。

Syntax: 句法:
variable <<= value;
         equivalent to:                 variable = variable << value;
           
Example: 例:
// C++ program to demonstrate the
// example of <<= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 0x0A;

    cout << "Before the operation, x = " << x << endl;
    x <<= 0x02;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 40
           

8)右移和指派運算符(>> =) (8) Right shift and assignment operator (>>=))

It shifts the value of the variable by the given number of bits (value) to the right and assigns the result to the variable.

它将變量的值向右移動給定位數(值),并将結果配置設定給變量。

Syntax: 句法:
variable >>= value;
         equivalent to:                 variable = variable >> value;
           
Example: 例:
// C++ program to demonstrate the
// example of >>= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 0x0A;

    cout << "Before the operation, x = " << x << endl;
    x >>= 0x02;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 2
           

9)按位與和指派運算符(&=) (9) Bitwise AND and assignment operator (&=))

It performs the Bitwise AND (&) operation on the existing value of the variable with the given value and assigns the result to the variable.

它對具有給定值的變量的現有值執行按位與(&)操作,并将結果配置設定給變量。

Syntax: 句法:
variable &= value;
         equivalent to:                 variable = variable & value;
           
Example: 例:
// C++ program to demonstrate the
// example of &= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 0x0A;

    cout << "Before the operation, x = " << x << endl;
    x &= 0x02;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 2
           

10)按位或與指派運算符(| =) (10) Bitwise OR and assignment operator (|=))

It performs the Bitwise OR (|) operation on the existing value of the variable with the given value and assigns the result to the variable.

它對具有給定值的變量的現有值執行按位或(|)操作,并将結果配置設定給變量。

Syntax: 句法:
variable |= value;
         equivalent to:                 variable = variable | value;
           
Example: 例:
// C++ program to demonstrate the
// example of |= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 0x0A;

    cout << "Before the operation, x = " << x << endl;
    x |= 0x02;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 10
           

11)按位XOR和指派運算符(^ =) (11) Bitwise XOR and assignment operator (^=))

It performs the Bitwise XOR (^) operation on the existing value of the variable with the given value and assigns the result to the variable.

它對具有給定值的變量的現有值執行按位XOR(^)操作,并将結果配置設定給變量。

Syntax: 句法:
variable ^= value;
         equivalent to:                 variable = variable ^ value;
           
Example: 例:
// C++ program to demonstrate the
// example of ^= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 0x0A;

    cout << "Before the operation, x = " << x << endl;
    x ^= 0x02;
    cout << "After the operation, x = " << x << endl;

    return 0;
}
           
Output: 輸出:
Before the operation, x = 10
After the operation, x = 8
           
C++ program to demonstrate the example of various assignment operators C ++程式示範各種指派運算符的示例
// C++ program to demonstrate the example
// of various assignment operators

#include <iostream>
using namespace std;

int main()
{
    int x = 0;

    // = operator
    x = 20;
    cout << "x = " << x << endl;

    // += operator
    x += 5;
    cout << "x = " << x << endl;

    // -= operator
    x -= 5;
    cout << "x = " << x << endl;

    // *= operator
    x *= 5;
    cout << "x = " << x << endl;

    // /= operator
    x /= 3;
    cout << "x = " << x << endl;

    // %= operator
    x %= 5;
    cout << "x = " << x << endl;

    // <<= operator
    x <<= 5;
    cout << "x = " << x << endl;

    // >>= operator
    x >>= 5;
    cout << "x = " << x << endl;

    // &= operator
    x &= 5;
    cout << "x = " << x << endl;

    // |= operator
    x |= 5;
    cout << "x = " << x << endl;

    // ^= operator
    x ^= 10;
    cout << "x = " << x << endl;

    return 0;
}
           
Output: 輸出:
x = 20
x = 25
x = 20
x = 100
x = 33
x = 3
x = 96
x = 3
x = 1
x = 5
x = 15
           

Recommended posts

推薦的文章

  • Arithmetic Operators in C/C++

    C / C ++中的算術運算符

  • Relational Operators in C/C++

    C / C ++中的關系運算符

  • Logical Operators in C/C++

    C / C ++中的邏輯運算符

  • Pre-increment and Post-increment Operators in C/C++

    C / C ++中的預增和後增運算符

  • sizeof() Operator Operands in C++ programming

    C ++程式設計中的sizeof()運算符操作數

  • C++ Alternative Operator Representations

    C ++替代運算符表示

  • C++ Operatots (new, delete, <>)

    C ++ Operatots(新增,删除,<>)

  • What is the value of sizeof('x') and type of character literals in C++?

    C ++中sizeof('x')的值和字元文字的類型是什麼?

  • Difference between new and malloc() in C++

    C ++中的new和malloc()之間的差別

  • Difference between delete and free() in C++

    C ++中delete和free()之間的差別

翻譯自: https://www.includehelp.com/cpp-tutorial/assignments-operators-in-c-cpp.aspx

c語言中指派運算符優先級