研究生复试之C语言

为了研究生复试中的编程部分来复习一遍C语言,发现大一学了C语言到现在已经忘记大部分。重新整理一遍基础知识。

头文件

C语言的头文件为#include<stdio.h> 其中stdio.h为标准输入输入库。而标准C++中没有.h后缀。并且使用using namespace std;来定义标识符的范围。

转义字符

  • \n:换行
  • \0:空字符Null
  • \t:Tab缩进

ASCLL编码

  • 48-57为0-9
  • 65-90为A-Z
  • 97-122为a-z

代码:包括200内所有的ASCLL编码对应的字符

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main() {
printf("%c\n",48);
printf("%d\n",'A');
printf("%d\n",'a');
for(int i=0;i<200;i++){
printf("%d->%c\t",i,i);
}
return;
}

结果:
运行结果

数据类型

数据类型 字节数 格式
int 2 %d
float 4 %f
double 8 %lf
long 4 %d
long long 4 %lld
char 1 %c
bool 1 0或1%d,true或false
  • bool类型需要自己定义或者添加头文件stdbool.h
  • C语言中没有String类型,字符串用字符数组存放,格式为%s。代码:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main() {
char a[5]="Hello";
char c[5]="Hello World";
char b[10]="Hello";
char d[10]="Hello World!";
printf("%s\n%s\n%s\n%s",a,c,b,d);
return 0;
}

结果

1
2
3
4
5
Hello
Hello
Hello
Hello Worl
--------------------------------
  • float单精度最大有效位数为6-7位,double双精度最大有效为数为15-16位。可自定义有效位数。如:
1
2
3
4
5
6
7
8
#include <stdio.h>
int main() {
float a = 1.12345678910;
double b = 2.12345678910;
printf("%.10f\n",a);
printf("%.10f\n",b);
return 0;
}

结果:

1
2
1.1234568357
2.1234567891
  • 强制类型转换

与Java类似

1
2
3
4
5
6
7
#include <stdio.h>
int main() {
float a = 1.12345678910;
printf("%.2f\n",a);
printf("%d\n",(int)a);
return 0;
}

结果:

1
2
1.12
1

其他

  • C语言中有宏定义其语法为:
1
#define PI 3.1415926
  • C语言中使用const等价于Java中final关键字

更多资料可查阅这里