C Primer Plus Study Notes (5) - Operators, Expressions and Statements

  • Basic operators:

A few terms: data objects, lvalues, rvalues, and operators

The purpose of an assignment expression statement is to store a value in a memory location. The data stores used to store values ​​are collectively called data objects, and the C standard only uses the term object when referring to this concept.

There are several ways to identify objects: using variable names, specifying elements of arrays, members of structures, using pointer expressions (the pointer stores the address of the object it points to)

An lvalue is a C language term used to identify a name or expression of a specific data object. Objects refer to the actual data storage, while lvalues ​​are labels used to identify or locate the storage location.

A new term has been added to the C standard: modifiable lvalue, which is used to identify a modifiable object. So, the left side of an assignment operator should be a modifiable lvalue, better to use the term "object-located value".

An rvalue is a quantity that can be assigned to a modifiable lvalue and is not itself an lvalue.

C uses operators to represent arithmetic operations, and the operand is the object that the operator operates on.

Assignment operator: =

In C language, the assignment operator is "="

a = 2; // assign value 2 to variable a

The left side of "=" is a variable name, and the right side is the value assigned to the variable

Addition operator: +

The addition operator is used for addition operations to add the values ​​on both sides of it

a = 1 + 1;

The added value can be a variable or a constant

int a = 1;
int b = 3;
c = a + b;

Minus operator:-

The minus operator is used for subtraction, subtracting the number on the left from the number on the right

a = 3 - 1;

Addition operator + and subtraction operator - are known as binary operators i.e. these operators require two operands to complete

Symbolic operators: - and +

The minus sign can also be used to identify or change the algebraic sign of a value

a = -1;
b = -a;

The plus sign does not change the value or sign of the operand

a = +1;

The plus and minus signs used in this way are called unary operators, i.e. only one operand is required

Multiplication operator: *

The symbol * means multiplication

a = 1 * 3;

Division operator: /

The symbol / means division, the value on the left of the / is the dividend, and the value on the right of the / is the divisor

The result of floating point division is a floating point number, the result of integer division is an integer, and an integer is a number without a fractional part.

In C, the fractional part of the result of integer division is discarded, a process called truncation

Integer division truncates the fractional part of the calculation result and does not round the result

The result of mixed integer and floating point calculations is a floating point number

In fact, the computer can't really divide a floating point number by an integer, the compiler will convert both operands to the same type, and the integer will be converted to a floating point number

#include <stdio.h>

int main(void)
{
	printf("integer division: 5/4 is %d \n", 5 / 4);
	printf("integer division: 6/3 is %d \n", 6 / 3);
	printf("floating division 5./4. is %1.2f \n", 5. / 4.);
	printf("mixed division 5./4 is %1.2f \n", 5. / 4);

	return 0;
}

operation result

For integer division of negative numbers, the method to deal with the fractional part is to truncate towards zero, that is, to directly discard the fractional part

-3.8 performs zero-trend truncation to convert to -3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649432&siteId=291194637