C语言

[[2.C语言Char和string]]

[[3.C语言指针]]

[[4.C语言函数]]

[[C语言代码集]]

[[C++速转Python]]

细节

main()函数

在最新的 C99 标准中,只有以下两种定义方式是正确的:

int main( void ) /* 无参数形式 */
{

return 0;
}
int main( int argc, char argv[] ) / 带参数形式 */
{

return 0;
}

在C90标准中也可以

int main(){

​ return 0;

}

变量

  • 变量名以英文字母开头

  • 不可以包含空格、标点符号和类型说明符(%、&、!、#、@、$)

  • 字母是区分大小写

    字符数组中如果分配的空间多余字符的数目,后面是用0填充的,通过验证可以知道这里是数字0而不是字符0

数据类型

signed与unsigned

signed的作用是:声明有符号类型的整数类型。

  • signed意思为有符号的,也就是第一个位代表正负,剩余的代表大小,例如:signed int 大小区间为-32768 到 +32767的整数

unsigned的作用是:声明无符号的整数类型。

  • unsigned意思为无符号的,所有的位都为大小,没有负数,例如:unsigned int 大小区间为:0到 的非负整数

Float 与Double

float和double都属于浮点数。区别在于:double所表示的范围,整数部分范围大于float,小数部分,精度也高于float

浮点数不可以直接用==!=比较

Char与String

char取值为[-127,127],如果超过127则为-128+(超出128的部分),如128=-128,129为-127

原因是129二进制是10000001,char存储八位,将第一位认为是符号位

string 是字符串,char是单个的字符。string相当于一个容器,char可以放在里面。string有结束符,char没有

char 用'' ,string用""

char数组的最后应该是'\0'变量

关键字

不能用作变量名

这些关键字如下:

auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while

Printf函数细节

%是C语言的格式符号

print type 描述
i or d signed integer
u unsigned integer//最高位不是符号位,没有符号
f,lf floating point number with”.”,标准是f
e floating point number with”e”(以指数方式输出实数)
o,x octal(八进制),hexadecimal(16进制)
c,s Character(仅输出最后一位), String
\n 换行
\t 将光标移到最接近8的倍数的位置

%.(e.g. %3.1f).

  1. 零标志
  2. 最小字段宽度(如果实际宽度大,就正常输出)
  3. 精度(小数点后有几位)
  4. 转换说明符

比如printf(“%7s%13s”,”tht”,”nb”);输出为“ tht nb”

tht前有4个空格,nb前有11个空格

在这里插入图片描述

在这里插入图片描述

标识符

字符 说明
- 结果左对齐,右边填空格;默认右对齐,左边填空格
+ 输出符号(正好或负号)
space 输出值为正时加上空格,为负时加上负号
# type是0,x,X时,增加前缀0,0x,0X
0 在输出前补上零,直到占满指定列宽为止

小数的输出 %e的用法

C语言中小数的指数形式为:aEn 或 aen

a 为尾数部分,是一个十进制数;n 为指数部分,是一个十进制整数;E或e是固定的字符,用于分割尾数部分和指数部分。整个表达式等价于 a×10n。

2.1E5 = 2.1×10^5,其中 2.1 是尾数,5 是指数。

3.7E-2 = 3.7×10^-2,其中 3.7 是尾数,-2 是指数。

switch语句

switch语句中的 expression 是一个常量表达式,必须是一个整型或枚举类型。

在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。

case 的 constant-expression 必须与 switch 中的变量具有相同的数据类型,且必须是一个常量或字面量。

当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。

当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。

不是每一个 case 都需要包含 break。如果 case 语句不包含 break,控制流将会 继续 后续的 case,直到遇到 break 为止。

一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。

1
2
3
4
5
6
7
8
9
char inchar='A';
switch(inchar){
case 'A': printf("A");
case 'B': printf("b");//将输出Ab
break;//没有break则将一直执行
case 'C':printf("c");
case 'D':
default:printf("no");//非必须
}

赋值语句作判断条件

它并不是以是否赋值成功作为true和false的判断机制,而是看赋值的值是多少,如果为0自动就作为false了
判断句优先级低于运算

循环

for循环

for(x=999;x>=1;x-=2)printf(“%d\n”,x);
//最后输出1,for循环先判断-2后是否满足,再进入循环

do while循环

do{
if(x%2==0)
printf(“%d\n”,x);
x+=2;
}while(x<100);//注意这里的“;”
//最后是98

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int main(){
float x=3.1415;
double y=3.1415,n=1234567.1234567;
int b=1234567;
char a='c';//可以有多个字符,但只看最后一个
char m[]="sbaxc";//char数组表示string,或者用char* m定义,用双引号,不然报错 ,
printf("%d\n",x==y);//0(double float 不能直接比较),==表示判断句
printf("%e\n",x);//3.141500e+000
printf("%lf limited decimal is %6.5lf\n",n,n);//lf默认六位,.后面表示小数点后的位数 (5),并且会四舍五入,.前面没有影响
printf("%c's ascill number is %d\n",a,a);//C输出字符与数字
printf("%s\n",m);//s输出字符串
printf("%1.2lf\n",99.996);//输出为100.00
int inte=1;
printf("%d\n",inte++);//++在后面,先输出,后+1
printf("%d\n",++inte);
printf("%.3e",2.71);//2.710e+00(补齐)
printf("%.3e",2.71855);//2.719e+00(四舍五入)
int j=5;
printf("%d%d%d",++j,j++,—j);//先+1后输出,后输出后+1,再-1后输出
return 0;
}

随机数

#include<stdlib.h>

rand()能产生伪随机数

srand(time(NULL)); //初始化随机过程

需要搭配#inlcude<time.h>

数组

a[3][4]

0,0 0,1 0,2 0,3
1,0 1,1 1,2 1,3
2,0 2,1 2,2 2,3

for(int i=1;i<=n;i++)

​ for(int j=1;j<=n;j++)a[i][j]

最终是一行一行扫下来

int matrix[4][3] = {[0][0]=1,[1][1]=5, [2][2]=9};

期末考复习

输入输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<string.h>
#include<stddef.h>
#include<stdlib.h>
int main(){
// //输出问题点
// int n=4,cd=4,de=60;
// float z=19.0;
// double z2=19.0;
int x = 19,y = 4;
float z = 19.0;
printf ("%d\n", x / y);//4
printf("%f\n", z / y);//4.750000 lf和f都是打出6位
printf("%f\n", x/(float)y);//4.750000
printf("%f\n", (float)(x / y));//4.000000,只有这种情况是除出为整数后输出
printf("%f\n", z / ++y);//3.800000 (先加,后除
printf("\n%.2f",z/cd);//4.75,.后面是几就打出几位
printf("\n%7s%13s","tht","nb");//tht前有4个空格,nb前有11个空格
printf("\n%8.2f",z);// 19.00小数点也算一格,前面三格
printf("\n%i",cd);//%i=%d
printf("\n%d %d %f",z,z2,n);//输出格式错全为0 (0 0 0.000000)
printf("\n%d",10e5);//e前后都必须有数字,并且e后面必须是整数,2.1E5 = 2.1×10^5,其中 2.1 是尾数,5 是指数

//输入问题点
//scanf中float用f,double用lf,long long用lld ,printf没有lf !!!
char c,*str;
scanf("%c",c);
scanf("%s",str);//注意字符串输入无地址符
char strr[20];
scanf("%[^\n]",strr);//注意这里变量必须是数组形式,这种输入可以接受空格


//switch case
char inchar='A';
switch(inchar){
case 'A': printf("A");
case 'B': printf("b");//将输出Ab
break;//没有break则将一直执行
case 'C':printf("c");
case 'D':
default:printf("no");//非必须
}
return 0;
}

下半学期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
#include<string.h>
#include<stddef.h>
#include<stdlib.h>
struct time{
struct date{
int ttt;
}tt;
int ti,tj,tq;
}t1,t[100];
struct node{
struct node* next;
int data;
};
int mystery(const int b[], int p) ; /* function prototype */
void swaps(char **a,char **b){
char *c=*a;
*a=*b;
*b=c;
}
void swapi(int *a,int *b){
int *c=*a;
*a=*b;
*b=c;
}
int calculateLength(char* a){
int cnt=0;
while(*a){
a++;
cnt++;
}
return cnt;
}
int main(){
//char,string复习
signed char c=128;
printf("%d\n",c);
int a=5,b=6;
swapi(&a,&b);
printf("%d %d\n",a,b);
// char *x="abc",*y="def";
// swaps(&x,&y);
// printf("%s %s",x,y);
printf("%d\n",calculateLength("abcdefg"));
char chr[100],s[4]=" ",*tok,*rev[20];
scanf("%[^\n]",chr);
int n=0,i;
// Use of strtok to get first token
tok = strtok(chr, s);
while (tok != 0) {
printf(" %s\n", tok);
rev[n]=tok;
n++;
tok = strtok(0, s);//!!!!!!千万注意这里是NULL或者0
}
for(i=n-1;i>=0;i--)printf("%s ",rev[i]);//反向输出
printf("\n");

//struct复习
struct time *ptr;
ptr=&t1;
//三种struct结构赋值方式
ptr->ti=5;
t1.ti=6;
t1.tt.ttt=4;//多重struct结构(nested)
struct time t1={//多重struct赋值
.tt.ttt=3,
.tj=2
};//顺序赋值
printf("%d \n",t1.tt.ttt);
struct tme t2={1,2,3};//顺序赋值
// struct tme t2={//无序赋值 (运行时注意注释前面)
// .tj=2,
// .ti=3,
// .tq=1
// };
printf("%d %d %d\n",t2.ti,t2.tj,t2.tq);
return 0;
}

C语言
http://mavericreate.top/Blogs/2022/09/05/C语言/
作者
唐浩天
发布于
2022年9月5日
许可协议