C language expression

Comma expression

逗号表达式:就是用逗号隔开的多个表达式,从左向右依次执行
exp1,exp2,wxp3,......wxpn

Note: The results of the whole expression is the result of the last expression.

void test()
{
	int a = 1;
	int b = 2;
	int c = (a > b, a = b + 10, b = a + 1);
	//可知最后一个表达式的结果,就是整个表达式的结果。
	//那么从左到右依次计算就会得到c = 13;
	
}

The following table references, function calls, and structure members

1. []: operator breaks the table reference

操作数 : 一个数组名 + 以后索引值
int arr[10];
arr[9] = 10;
[ ]:有两个操作数一个是arr,另外一个就是9.
注意:下表引用常用与数组的遍历修改

2. (): function call operator

操作数 : 一个函数名 + [若干传递给函数的参数] 
#include<stdio.h>
void test()
{
	printf("hehe\n");
}

void test2(const char* str)
{
	printf("%s\n",str);
}

int main()
{
	test();
	test2("hello worl");
	return 0;
}

3. To access a structure member

. :结构体.成员名
-> :结构体指针->成员名

#include<stdio.h>
struct stu{
	char name[20];
	int age;
};
void test(struct stu stu)
{
	stu.age = 18;
}

void test2(struct stu * pstu)
{
	pstu->age = 18;
}

itn main()
{
	struct stu stu;
	struct stu* pstu = &stu;
	
	stu.age = 10;
	test(stu);
	
	pstu->age = 20;
	test2(pstu);
	return 0;
}

Expression evaluation

Known to evaluate the expression, there is a part of operator precedence and associativity decisions, but there is a part in the process of job needs to be performed before the cast can be calculated.
Implicit type conversion:
About implicit conversion means occur in the absence of a clear explanation of (C language style casts is part of our programmers have clearly explained), the compiler automatically casts to help us perform. Usually the same type of data calculation, comparison and assignment when we do not need to worry about, I'm just here illustrate the different types of data calculation, comparison and assignment, and we did not when programmers cast, the compiler is how to specify we help convert between types of treatment , only know the process, we should pay attention to let us know when and where to avoid programming.

(1) is smaller than the int type implicit type conversion: Integer lift (Integer Promotion).

Shaping the C language arithmetic always default to the shaping accuracy, so in order to obtain this accuracy, the character and short-integer operand expression is converted into an ordinary integer prior to use, this conversion is called integral promotion

(2) Why integral promotion (Integer Promotion)?

We know that the expression shaping operation to be completed in the corresponding operation device of the CPU, and this period is the logic unit (ALU) We also called shaper operator, the ALU operand length is a byte length is 32 int bit is also general-purpose register is the length of cpu. So if we want this time is calculated by adding two types of char, while the CPU is executing in 32-bit standard vertebral length, but only 8 char, if packets must first be calculated becomes a char 32 then ALU register into the calculation. Therefore, the expression is smaller than an int type will be converted to int or unsigned int, to be calculated. And this conversion process is shaping upgrade , which is shaped to enhance the meaning of existence ;;;;

(3) how to enhance the shaping

Shaping in accordance with the sign bit data to enhance the type of variable to ascend

//负数的提升
char c1 = -1;变量c1的二进制位(补码)中只有8个
比特位:11111111
因为char为有符号的char,所以整形提升时,高位补
符号位,
提升之后:11111111111111111111111111111111


//正数的整形提升
char c2 = 1;变量c1的二进制位(补码)中只有8个
比特位:00000001
因为char为有符号的char,所以整形提升时,高位补
符号位,
提升之后:00000000000000000000000000000001

//无符号整形提升,,/*高位补0*/就OK了


//栗子
void test()
{
	char c = 1;
	printf("%u\n", sizeof(c));//输出1
	printf("%u\n", sizeof(+c));//输出4
	printf("%u\n", sizeof(!c));//输出1
}
//只要c参加表达式运算就会发生整形提升,所以+c就会发送提升,所以输出4,而其他没有发生表达式运算
(4) when assignment implicit type conversion

Mentioned above calculation formulas are implicit type conversion, but also during special note is the assignment, right data types assignment operator must be converted to the left of the assignment type of the number, if the data type of the right length is greater than the left, will have truncation or rounding.

char ch;
int i,result;
float f;
double d;
result = ch/i + (f*d-i);

(1)首先计算 ch/i,ch → int型,ch/i → int型。

(2)接着计算 f*d-i,由于最长型为double型,故f→double型,i→double型,f*d-i→double型。

(3)(ch/i)(f*d-i)进行加运算,由于f*d-i为double型,故ch/i→double型,ch/i+(f*d-i)double型。

(4)由于result为int型,故ch/i+(f*d-i)doubleint,即进行截断与舍入,最后取值为整型
Published 41 original articles · won praise 15 · views 8841

Guess you like

Origin blog.csdn.net/baidu_41844500/article/details/104716881