- You can see the manual: c language - operator < / font > < / a > the contents of the < / li >
I want to ask how C language operators implement operations, such as a+b, why can calculate two numbers, and what type of operator is
0 Answer
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.
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.
Thebinary 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
The
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.
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:
Arithmetic operators: including plus +, minus -, multiplication *, division /, etc., used for numerical computation.
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.
logical operators: including & & , or | |, non! Etc., which is used to perform logical operations on conditions.
bit operators: including bitwise and & , bitwise or |, bitwise take ~, left & lt; < , right shift > > Etc., used to operate on binary bits.
Assignment operator: includes equal sign =, addition +=, subtraction -=, multiplication *=, division /=, etc., used to assign values to variables or perform simple calculations.
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.
这家伙很懒，什么都没留下...