Operators and expressions in C language

Table of contents

basic arithmetic operators

Operator precedence and associativity

cast



 

basic arithmetic operators


In C language, there are the following basic arithmetic operators:

1. Addition operator (+): used to add two numbers.
   Example: `int sum = 5 + 3; // The result is 8`

2. Subtraction operator (-): used to subtract one number from another number.
   Example: `int difference = 7 - 2; // The result is 5`

3. Multiplication operator (*): used to multiply two numbers.
   Example: `int product = 4 * 6; // The result is 24`

4. Division operator (/): used to divide one number by another number. Note that integer division truncates the decimal part, leaving only the integer part.
   Example: `int quotient = 10 / 3; // The result is 3`

5. Modulo operator (%): used to calculate the remainder after dividing one number by another number.
   Example: `int remainder = 10 % 3; // The result is 1`

These arithmetic operators can be used to perform basic mathematical operations, and you can combine them as needed to create more complex mathematical expressions. For example, you can write a C statement like this:

```c
int result = (5 + 3) * (7 - 2); // The result is 40
```

In this example, we used the addition, subtraction, and multiplication operators to perform multiple operations and combined them into an expression to calculate the result.

Operator precedence and associativity


Operators in C language have different precedence and associativity. These rules determine the order in which multiple operators are evaluated when they appear in an expression. Here is an overview of the precedence and associativity of some common C language operators:

1. **Associativity**: Specifies how operators are combined between operators of the same precedence in an expression. Most operators in C are left-to-right associative, which means they are evaluated from left to right. For example, `a + b + c` will be evaluated from left to right, first calculating `a + b`, then adding the result to `c`.

2. **Priority**: Different operators have different priorities. Operators with higher priority are evaluated in expressions before operators with lower priority. Here are the precedences of some common operators (from highest to lowest):

   - Parentheses `()`: Parentheses have the highest precedence and can be used to change the default calculation order.
   - Unary addition and subtraction `+` `-`: for positive and negative numbers.
   - Multiplication `*` and division `/`: Multiplication and division have equal priority, higher than addition and subtraction.
   - Addition `+` and subtraction `-`: Addition and subtraction have equal priority, lower than multiplication and division.
   - Relational operators (for example, `<`, `>`, `<=`, `>=`, `==`, `!=`): used to compare values.
   - Logical AND `&&`: used for logical AND operations.
   - Logical or `||`: used for logical or operation.
   - Assignment operators (for example, `=`, `+=`, `-=`, `*=`, `/=`, etc.): used to assign values ​​to variables.

Note that if you are unsure of the order in which an expression will be evaluated, you can use parentheses to explicitly specify precedence. For example, `(a + b) * c` will ensure that `a + b` is calculated first before multiplying the result with `c`.

In addition, C language also has unary operators, bitwise operators and other operators, which also have their own precedence and associativity rules. Making sure you understand these rules is important to correctly understand and write complex expressions. Please refer to the C language standard documentation or programming textbooks for detailed information.

cast


In C language, Type Casting is an operation that converts a value of one data type into a value of another data type. This can be useful in certain situations, such as when you need to perform operations between different data types, or need to ensure that the result of an expression has a specific data type.

The C language provides two types of casts:

1. **Explicit Type Casting** (Explicit Type Casting): This is a type conversion explicitly specified by the programmer, usually using the cast operator. There are two main cast operators in C language:

   - **(type) expression**: This is the most common form, where type represents the target data type to be converted to and expression is the expression to be converted. For example, `(int) 3.14` converts the floating point number 3.14 to an integer, and the result is 3.

   - **type(expression)**: This is another form, where type is the target data type to be converted to and expression is the expression to be converted. For example, `int(3.14)` also converts the floating point number 3.14 to an integer, and the result is 3.

   These explicit casts can be used to cast a value of one data type to a value of another data type, but use caution as it can result in data loss or inaccuracy.

2. **Implicit Type Conversion** (Implicit Type Conversion): Sometimes, the C language automatically performs type conversion to make operations on different data types possible. This type conversion is also called "type promotion" or "automatic type conversion". For example, when an integer and a floating point number are added, C promotes the integer to a floating point number and then performs the addition operation. This type conversion is performed automatically based on a set of rules.

Here are some examples of casts:

```c
float pi = 3.1415926;
int integerPi = (int)pi; // Explicitly convert floating point number to
integer```

It should be noted that when performing casts, you should ensure that the conversion is safe and reasonable to avoid data loss or unexpected behavior. Casting can be used to solve specific problems, but it should be used with caution and to ensure that its potential impact is understood.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132913469