2020/3/26 C language a fraction of finishing notes

2020/3/26

Numeric overflow

  • If a variable of type int, has achieved the maximum value (2147483647), give this variable +1. It will overflow.
    • Overflow: maximum +1
    • Underflow: Min -1

Unsigned

  • Range: 0 to 2 32 -1
  • The maximum value of + 1 -> 0
  • 0-1--> MAX

Have signed

  • Overflow: maximum value + 1 -> Min
  • Underflow: Min -1 -> maximum

summary

  • Most compilers, for numeric overflow, with the above approach. However, the individual compiler may not.
  • When the influence by the value overflows, data storage, to select the appropriate data type. 389482390483209 --int

Not commonly used keyword

  • extern: express statement. The statement did not memory space. It can not be promoted to define.

  • const: a variable restriction is read-only. constant.

  • Volatile: prevents compiler optimizations.

  • register: a register variable is defined, no memory address, to improve efficiency. register is the recommended type of instruction, not an order type of instruction, if there is an idle CPU registers, then the register will take effect if there is no free register, the register is invalid.

Input and output functions

Output function

printf output is a string, putchar outputs a char.

printf format characters

Print format Corresponding data type meaning
%d int It takes an integer value and expressed as a signed decimal integer
%hd short int Short integer
% Hu unsigned short Unsigned short integer
%O unsigned int Unsigned octal integer
in% unsigned int Unsigned decimal integer
%x,%X unsigned int Unsigned hexadecimal integers, x corresponds abcdef, X corresponds ABCDEF
%f float Single-precision floating-point number
%lf double Double-precision floating-point number
% E,% E double Represents the number of scientific notation, where "e" is represented by a case when the output "e" capitalization
%c char Character. Can according to the digital input code corresponding to ASCII characters into a corresponding
%s char * String. Output string of characters until a null character string (string '\ 0' ends, this '\ 0' i.e. null character)
%p void * The output pointer 16 in hexadecimal form
%% % The output of a percent sign

Additional printf format:

character meaning
l (the letter l) Prepended d, u, x, o, long integer
- Left
m (represents an integer) Minimum width data
0 (number 0) The front fill output 0 until filled up to the specified width of a column can not be used with -
m, n (represents an integer) m refers to the wide-field, i.e., the number of entries corresponding to the character output on the output device occupied. n refers to the accuracy, for explaining the output of the real number of decimal places. Type is numerical, when n is not specified, an implicit precision of n = 6 bits.

Input function

  • char getchar is read from a standard input device.

  • scanf% can be escaped by way of the user data input from the input device standard.

#include <stdio.h>

int main()
{
	char ch1;
	char ch2;
	char ch3;
	int a;
	int b;

	printf("请输入ch1的字符:");
	ch1 = getchar();
	printf("ch1 = %c\n", ch1);

	getchar(); //测试此处getchar()的作用

	printf("请输入ch2的字符:");
	ch2 = getchar();
	printf("\'ch2 = %ctest\'\n", ch2);

	getchar(); //测试此处getchar()的作用
	printf("请输入ch3的字符:");
	scanf("%c", &ch3);//这里第二个参数一定是变量的地址,而不是变量名
	printf("ch3 = %c\n", ch3);

	printf("请输入a的值:");
	scanf("%d", &a);
	printf("a = %d\n", a);

	printf("请输入b的值:");
	scanf("%d", &b);
	printf("b = %d\n", b);

	return 0;
}

Operators and Expressions

Operators commonly classified

Operator Type effect
Arithmetic Operators For processing four arithmetic operations
Assignment Operators Value of the expression is used to assign variables
Comparison Operators For comparison expression, and return a true or false value
Logical Operators It used to return true or false value depending on the value of the expression
Bitwise Operators Bit operation for data processing
sizeof operator Length number of bytes to seek

Arithmetic Operators

Operators the term
+ Positive sign
- negative
+ plus
- Less
* Multiply
/ except
% Modulo (remainder)
a++ Before the increment
++a After the increment
a-- Pre-decrement
--a Since Less

Assignment Operators

Operators the term
= Assignment
+= Plus equal
-= Less equal
*= Take equal
/= In addition to equal
%= Mold equal

Comparison Operators

Comparison operation in the C language, "true" with the number "1" indicates "false" with the number "0" is represented.

Operators the term
== equal to
!= not equal to
< Less than
> more than the
<= Less than or equal
>= greater or equal to

Logical Operators

Operators the term Examples result
! non- !a ! If a false, a true;! If a is true, then a false.
&& versus a && b If a and b are true, the result is true, otherwise false.
|| or a || b If a and b have a true, the result is true, both are false, the result is false.

Operator Precedence

priority Operators Name or meaning Use the form Joining direction Explanation
1 [ ] Array subscript Array name [constant expression] Left to right --
() Parentheses (Expression) / function name (parameter list) --
. Member selection (objects) Object. Member Name --
-> Member selection (pointer) Object pointer -> member name --
2 - Operators negative -expression Right to Left Unary operator
~ Bitwise operator ~ Expression
++ Increment operator ++变量名/变量名++
-- 自减运算符 --变量名/变量名--
* 取值运算符 *指针变量
& 取地址运算符 &变量名
! 逻辑非运算符 !表达式
(类型) 强制类型转换 (数据类型)表达式 --
sizeof 长度运算符 sizeof(表达式) --
3 / 表达式/表达式 左到右 双目运算符
* 表达式*表达式
% 余数(取模) 整型表达式%整型表达式
4 + 表达式+表达式 左到右 双目运算符
- 表达式-表达式
5 << 左移 变量<<表达式 左到右 双目运算符
>> 右移 变量>>表达式
6 > 大于 表达式>表达式 左到右 双目运算符
>= 大于等于 表达式>=表达式
< 小于 表达式<表达式
<= 小于等于 表达式<=表达式
7 == 等于 表达式==表达式 左到右 双目运算符
!= 不等于 表达式!= 表达式
8 & 按位与 表达式&表达式 左到右 双目运算符
9 ^ 按位异或 表达式^表达式 左到右 双目运算符
10 | 按位或 表达式|表达式 左到右 双目运算符
11 && 逻辑与 表达式&&表达式 左到右 双目运算符
12 || 逻辑或 表达式||表达式 左到右 双目运算符
13 ?: 条件运算符 表达式1?表达式2: 表达式3 右到左 三目运算符
14 = 赋值运算符 变量=表达式 右到左 --
/= 除后赋值 变量/=表达式 --
*= 乘后赋值 变量*=表达式 --
%= 取模后赋值 变量%=表达式 --
+= 加后赋值 变量+=表达式 --
-= 减后赋值 变量-=表达式 --
<<= 左移后赋值 变量<<=表达式 --
>>= 右移后赋值 变量>>=表达式 --
&= 按位与后赋值 变量&=表达式 --
^= 按位异或后赋值 变量^=表达式 --
|= 按位或后赋值 变量|=表达式 --
15 逗号运算符 表达式,表达式,… 左到右 --

类型转换

数据有不同的类型,不同类型数据之间进行混合运算时必然涉及到类型的转换问题。

转换的方法

自动转换(隐式转换):

  • 遵循一定的规则,由编译系统自动完成。(小类型转大类型)

#include <stdio.h>
int main()
{
	int num = 5;
	printf("s1=%d\n", num / 2);
	printf("s2=%lf\n", num / 2.0);

	return 0;
}
  • 由赋值产生。

    • 小类型——>大类型:没问题

    • 大类型——>小类型:可能丢失数据

      • VS中Ctrl+F7 只编译,不运行。
    • 321:

      • 0000 0000 0000 0000 0000 0001 0100 001——二进制表达方式

      • 0000 0000 ——char只有一个字节

      • 赋值后,char值为:0100 0001 —— 65

强制类型转换:

强制类型转换指的是使用强制类型转换运算符,将一个变量或表达式转化成所需的类型。

#include <stdio.h>

int main()
{
	float x = 0;
	int i = 0;
	x = 3.6f;

	i = x;			//x为实型, i为整型,直接赋值会有警告
	i = (int)x;		//使用强制类型转换

	printf("x=%f, i=%d\n", x, i);

	return 0;
}

类型转换的原则

占用内存字节数少(值域小)的类型,向占用内存字节数多(值域大)的类型转换,以保证精度不降低。

程序流程结构

概述

C语言支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构。

  • 顺序结构:程序按顺序执行,不发生跳转。

  • 选择结构:依据是否满足条件,有选择的执行相应功能。

  • 循环结构:依据条件是否满足,循环多次执行某段代码。

选择结构

1、if语句

#include <stdio.h>

int main()
{
	int a = 1;
	int b = 2;

	if (a > b)
	{
		printf("%d\n", a);
	}

	return 0;
} 

2、if…else语句

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;

	if (a > b)
	{
		printf("%d\n", a);
	}
	else
	{
		printf("%d\n", b);
	}
	return 0;
}

3、if…else if…else语句

#include <stdio.h>

int main()
{
	unsigned int a;
	scanf("%u", &a);

	if (a < 10)
	{
		printf("个位\n");
	}
	else if (a < 100)
	{
		printf("十位\n");
	}
	else if (a < 1000)
	{
		printf("百位\n");
	}
	else
	{
		printf("很大\n");
	}

	return 0;
}

4、三目运算符

#include <stdio.h>
int main()
{
	int a = 10;
	int b = 20;
	int c;

	if (a > b)
	{
		c = a;
	}
	else
	{
		c = b;
	}
	printf("c1 = %d\n", c);
	a = 1;
	b = 2;
	c = ( a > b ? a : b );
	printf("c2 = %d\n", c);
	return 0;
}

5、switch语句

#include <stdio.h>
int main()
{
	char c;
	c = getchar();

	switch (c) //参数只能是整型变量
	{
	case '1':
		printf("OK\n");
		break;//switch遇到break就中断了
	case '2':
		printf("not OK\n");
		break;
	default://如果上面的条件都不满足,那么执行default
		printf("are u ok?\n");
	}
	return 0;
}

循环结构

1、while语句

#include <stdio.h>
int main(void)
{
	int a = 20;
	while (a > 10)
	{
		scanf("%d", &a);
		printf("a = %d\n", a);
        a/=10;
	}
	return 0;
}

2 、do…while语句

#include <stdio.h>
int main(void)
{
	int a = 1;
	do
	{
		a++;
		printf("a = %d\n", a);
	} while (a < 10);

	return 0;
}

3、for语句

for(;;){
    ······
}

#include <stdio.h>
int main(void)
{
	int i;
	int sum = 0;
	for (i = 0; i <= 100; i++)
	{
		sum += i;
	}
	printf("sum = %d\n", sum);
	return 0;
}

4、嵌套循环

循环语句之间可以相互嵌套。

#include <stdio.h>

int main(void)
{
	int num = 0;
	int i, j, k;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			for (k = 0; k < 10; k++)
			{
				printf("hello world\n");
				num++;
			}
		}
	}

	printf("num = %d\n", num);

	return 0;
}

跳转语句break、continue、goto

break语句

在switch条件语句和循环语句中都可以使用break语句:

  • 当它出现在switch条件语句中时,作用是终止某个case并跳出switch结构。

  • 当它出现在循环语句中,作用是跳出当前内循环语句,执行后面的代码。

  • 当它出现在嵌套循环语句中,跳出最近的内循环语句,执行后面的代码。

#include <stdio.h>

int main()
{
	int i = 0;
	while (1)
	{
		i++;
		printf("i = %d\n", i);

		if (i == 10)
		{
			break; //跳出while循环
		}
	}
	int flag = 0;
	int m = 0;
	int n = 0;
	for (m = 0; m < 10; m++)
	{
		for (n = 0; n < 10; n++)
		{
			if (n == 5)
			{
				flag = 1;
				break; //跳出for (n = 0; n < 10; n++)
			}
		}
		if (flag == 1)
		{
			break; //跳出for (m = 0; m < 10; m++)
		}
	}

	return 0;
}

continue语句

在循环语句中,如果希望立即终止本次循环,并执行下一次循环,此时就需要使用continue语句。

#include<stdio.h>

int main()
{
	int sum = 0;           //定义变量sum

	for (int i = 1; i <= 100; i++)
	{
		if (i % 2 == 0)   //如果i是一个偶数,执行if语句中的代码
		{
			continue;      //结束本次循环
		}
		sum += i;          //实现sum和i的累加
	}

	printf("sum = %d\n", sum);

	return 0;
}

goto语句(无条件跳转,尽量少用)

#include <stdio.h>
int main()
{
	goto End; //无条件跳转到End的标识
	printf("aaaaaaaaa\n");

End:
	printf("bbbbbbbb\n");

	return 0;
}

Guess you like

Origin www.cnblogs.com/CNLLB/p/12578106.html