c语言基本知识汇总
c语言基本数据类型
c语言基本数据类型:
数字类型 | 非数字类型 |
---|---|
整型(int)实型(float、double) | 字符型(char) |
其中每种数据类型还分signed和unsigned,short和long等,实际常用的就是上面表格列举的基本类型。
注意bool型不是C语言的数据类型,它是c++语言特有的类型。
c++比c多的数据类型:bool型、类类型class、引用类型&;
C语言常用的派生类型包括数组、指针、结构体类型struct。
为什么会有转义字符
为了文档编辑的方便需要回车符、制表符等,这些抽象的文档符号都没有直接对应的字符,所以需要转义字符加以表示。同时输出语句(printf)的设计使得需要某些转义字符,比如"表示双引号,\表示反斜杠等。
c语言的输入输出
单个变量的输入输出
单个基本变量的输入输出使用scanf()和printf();具体例子如下:
#include<stdio.h>
int main(void)
{
int i;
char c;
float f;
double d;
printf("input int char float double:");
scanf("%d %c %f %lf", &i,&c,&f,&d);
printf("output int: %d\n", i);
printf("output char: %c\n", c);
printf("output float: %f\n", f);
printf("output double: %f\n", d);
system("pause");
return 0;
}
输出结果如下
input int char float double:9 a 3.4 4.555
output int: 9
output char: a
output float: 3.400000
output double: 4.555000
注意输入double类型时需要使用**%ld**类型说明符。
问:为什么C语言的输入输出需要使用类型说明符呢?
输入需要类型说明符容易理解,需要为变量分配内存,需要知道变量的类型。
单个字符的常用输入输出方式
getchar、putchar函数
int main(void)
{
char c;
while ((c = getchar()) != EOF)
putchar(c);
system("pause");
return 0;
}
输出结果:
hurongping
hurongping
huqingshun
huqingshun
C语言的强制类型转化
在变量前面加上括号和需要强制转化的类型。
C语言字符串的输入和输出
C语言中字符串就是字符数组,以‘\0’结尾作为标记,使用起来极为不便。忍不住开始怀念c++的string和Python不区分字符串和字符。
讲一点底层的东西吧,C语言的字符是怎么存储的。计算机实际上只能表示数,而字符是通过ASCII表一一和一个整数对应的。所以char类型实际上存储的是整数,所以char类型也被称为整型。输出时还原对应的字符含义输出即可。
字符串的输入输出可以通过scanf()、printf()以及类型说明符%s实现
#include<stdio.h>
#define LEN 20
int main(void)
{
char c[LEN];
scanf("%s", c);
printf("output string:%s\n", c);
system("pause");
return 0;
}
输出结果:
hurongping
output string:hurongping
C语言字符串常用函数
字符串处理函数都在头文件string.h中声明
- strlen()函数返回字符串的大小,不包括结尾‘\0’的大小。既可以计算字符常量的长度,也可以计算字符数组的长度。
- strchr()函数在一个字符串中查询是否存在某个字符,存在返回第一次出现的指针,不存在返回NULL。
#include<stdio.h>
#include<string.h>
#define LEN 20
int main(void)
{
char c[LEN];
scanf("%s", c);
printf("output string:%s\n", c);
printf("1:----------strlen()-------------\n");
//strlen统计字符串常量的长度
printf("the len of \"string\":%d\n", strlen("string"));
//strlen统计字符数组的长度
printf("the len of string:%d\n", strlen(c));
printf("2:----------strchr()-------------\n");
//strchr返回字符第一次出现的指针
if (strchr(c, 'h') != NULL) {
printf("char 'h' index of string c:%c\n", *(strchr(c, 'h')));
}
else {
printf("char 'h' not in string c!\n");
}
system("pause");
return 0;
}
输出结果:
hurongping
output string:hurongping
1:----------strlen()-------------
the len of "string":6
the len of string:10
2:----------strchr()-------------
char 'h' index of string c:h
- strcpy()函数复制字符串,包括常量字符串和字符数组
- strcmp()函数比较字符串,比较规则是根据ASCII大小
- strcat()函数连接两个字符串
#include<stdio.h>
#include<string.h>
#define LEN 20
int main(void)
{
char c[LEN],d[LEN];
scanf("%s", c);
printf("1:----------strcpy()-------------\n");
//strcpy复制字符串常量
strcpy(d, "string");
printf("the copy of \"string\":%s\n",d);
//strcpy复制字符数组
strcpy(d, c);
printf("the copy of c:%s\n", d);
printf("2:----------strcmp()-------------\n");
//strcmp比较字符串大小
if (strcmp("string", "strstr") > 0) {
printf("\"string\" bigger than \"strstr\"\n");
}
else {
printf("\"string\" less than \"strstr\"\n");
}
printf("3:----------strcat()-------------\n");
//strcat连接字符串
strcpy(c, "hu");
strcpy(d, "rongping");
strcat(c, d);
printf("after d linked c is:%s\n", c);
system("pause");
return 0;
}
输出结果:
huronping
1:----------strcpy()-------------
the copy of "string":string
the copy of c:huronping
2:----------strcmp()-------------
"string" less than "strstr"
3:----------strcat()-------------
after d linked c is:hurongping
编程必备工具------C语言标准库函数使用
- <stdio.h>常用输入输出函数如printf()、scanf()、getchar()、putchar()等
- <ctype.h>测试字符的函数isdigit()、islower()、isupper()、isalpha()、tolower()、toupper()
- <string.h>包含常见的字符串处理函数,strlen()、strcpy()、strcmp()、strcat()、strchr()
- <math.h>常用数学函数
- <stdlib.h>包含内存分配函数malloc()、free()等
- <time.h>中包含的关于统计程序用时;
//循环累计求和
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int i = 1,n, sum=0;
scanf("%d", &n);
while (i <= n){
sum += i;
i++;
}
printf("%d\n", sum);
printf("time:%.16f\n", (double)clock() / CLOCKS_PER_SEC);
system("pause");
return 0;
}
为了避免控制窗口计入输入时间,采取命令行管道的输入方式。
命令行输出结果:
D:\vs2019workdir\算法竞赛入门经典\算法竞赛入门经典\Debug>echo 1000000|home1-3
1784293664
time:0.0020000000000000
//公式求和
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int n,sum;
scanf("%d", &n);
sum = (1+n)*n/2;
printf("%d\n", sum);
printf("time%.16f\n", (double)clock() / CLOCKS_PER_SEC);
system("pause");
return 0;
}
输出结果:(结果因为乘法溢出了)
D:\vs2019workdir\算法竞赛入门经典\算法竞赛入门经典\Debug>echo 1000000|home1-3
-363189984
time0.0000000000000000
- <limit.h>和<float.h>中保持了一些数据类型能存放的最大数
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
int main(void)
{
printf("%.0g %.0g\n", FLT_MAX,DBL_MAX);
system("pause");
return 0;
}
输出结果:
3e+38 2e+308