A small problem with C language operators

dd5323400 注册会员
2023-02-28 05:01

This answer quotes ChatGPT

In C, operators are special symbols used to perform operations. At the bottom of the computer, the CPU maps these operators into binary instructions and performs calculations.

For the addition operator "+", when the a+b operation is performed, the C compiler converts it into binary instructions, which, when executed in the CPU, reads the values of a and b from memory into the CPU registers, then adds the two values, and finally stores the result in the specified memory location. Of course, the exact implementation details will be affected by the hardware environment, such as the compiler, CPU, and so on.

In C language, operators belong to different types, mainly the following types:

1. Arithmetic operators: Include operators such as addition(+), subtraction(-), multiplication( ), division(/), and complement(%) for basic arithmetic calculations. 2. Relational operators: including equal to(==), not equal to(! =), greater than(>) , less than(<) , greater than or equal to(> =) and less than or equal to(< =) to compare the size relationship between two values.
3. Logical operators: include and(& &) , or(| |) and not(!) The etc operator, which handles logical relationships.
4. Bit operators: including bits and(&) , the bitwise or(|), or(^), left(& lt; <) And right shift(> >) The etc operator, which operates on binary bits. < br / > 5, the assignment operator, including simple assignment(=), and compound assignment operator(+ =, =, < / em > =, = / =, %, & amp; =, | = ^ =, & lt; < =, > > =), which is used to assign a value to a variable or to perform an operation on a variable before assigning a value.
6. In addition, there are other types of operators, such as the conditional operator(? :), the pointer operator(& , *), for specific syntactic structures or operations.

In short, C operators are special symbols used to perform operations, and by mapping these operators to binary instructions, you can eventually implement operations on variables. At the same time, operators in C belong to different types and are used for different operations and syntactic structures.

duqiangqq 注册会员
2023-02-28 05:01

Part of this answer references GPT, GPT_Pro to solve the problem better.
Operators in C are calculated according to specific rules, because different operators have different rules. So the C language divides operators into different types. There are mainly unary operators, binary operators and ternary operators.


unary operator requires only one operand to complete the operation, such as taking the inverse "!" , take the address "&" , the value is *, and the plus or minus sign is +/-. For example, suppose we have a variable a whose value is 10, and we want its inverse:

int a=10;
int b=!a;

In this case, the value of b is the value of a inverted 0.


binary operator requires two operands to complete operations, such as addition, subtraction, multiplication, division, greater-than-less, logical and or, and bit operations. For example, suppose we have a variable a and a variable b whose values are 10 and 5, respectively, and we want to add them:

int a=10;
int b=5;
int c=a+b;  // c的值就是15

ternary operator is implemented by three operands. There are three operands in an expression: conditional, true, and false. For example:

int a=10; 
int b=(a>9)?100:200;  // b的值就是100 

In the above expression, the conditional expression is a> 9, the true expression is 100, the false expression is 200. If a> If 9 is true(i.e. a is greater than 9), the value of true expression 100 is assigned to b; If a> If 9 is false(that is, a is less than or equal to 9), assign the value of the false expression 200 to b.
If the answer is helpful, please accept it.

cyk5597 注册会员
2023-02-28 05:01

In C, an operator is a symbol or keyword used to accomplish a specific operation. C operators include arithmetic operators, assignment operators, comparison operators, logical operators, and so on.

For arithmetic operators, such as the plus sign +, it can add two numbers, the underlying implementation is handled by the compiler. When compiling the source code, the compiler generates the corresponding assembly code to perform the addition operation according to the data types of the left and right operands of the plus sign. In computers, addition is done by the ALU(arithmetic logic Unit), which adds numbers via electronic circuits and stores the results in registers.

As for the types of operators, operators in C can be divided into the following categories:

  1. Arithmetic operators: including plus +, minus -, multiplication *, division /, etc., used for numerical computation.

  2. relational operators: including the less than sign < , greater than the number > , equals sign ==, does not equal sign! =, etc., used to determine the size relationship between two numbers.

  3. logical operators: including & & , or | |, non! Etc., which is used to perform logical operations on conditions.

  4. bit operators: including bitwise and & , bitwise or |, bitwise take ~, left & lt; < , right shift > > Etc., used to operate on binary bits.

  5. Assignment operator: includes equal sign =, addition +=, subtraction -=, multiplication *=, division /=, etc., used to assign values to variables or perform simple calculations.

  6. Other operators: including conditional operators? :, comma operator, etc.

In general, operators in C are classified according to the type of operand and the function of the operator. In a program, proper use of operators can improve the readability and efficiency of your code.

About the Author

Question Info

Publish Time
2023-02-28 05:01
Update Time
2023-02-28 05:01