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中声明

  1. strlen()函数返回字符串的大小,不包括结尾‘\0’的大小。既可以计算字符常量的长度,也可以计算字符数组的长度。
  2. 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
  1. strcpy()函数复制字符串,包括常量字符串和字符数组
  2. strcmp()函数比较字符串,比较规则是根据ASCII大小
  3. 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语言标准库函数使用

  1. <stdio.h>常用输入输出函数如printf()、scanf()、getchar()、putchar()等
  2. <ctype.h>测试字符的函数isdigit()、islower()、isupper()、isalpha()、tolower()、toupper()
  3. <string.h>包含常见的字符串处理函数,strlen()、strcpy()、strcmp()、strcat()、strchr()
  4. <math.h>常用数学函数
  5. <stdlib.h>包含内存分配函数malloc()、free()等
  6. <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
  1. <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
C语言基础知识点汇总包括以下内容: 1. 数据类型:C语言提供了基本的数据类型,如整数类型(int、short、long)、浮点类型(float、double)、字符类型(char)等。 2. 变量和常量:在C语言中,可以使用变量来存储和操作数据,而常量是固定的值。 3. 运算符:C语言支持各种运算符,包括算术运算符(+、-、*、/)、关系运算符(>、<、==、!=)、逻辑运算符(&&、||、!)等。 4. 控制语句:C语言提供了控制程序流程的语句,如条件语句(if-else)、循环语句(for、while、do-while)、跳转语句(break、continue、goto)等。 5. 数组:数组是一组相同类型的数据元素的集合,在C语言中使用方括号来定义和操作数组。 6. 函数:函数是一段可重用的代码块,可以通过函数名调用并执行其中的代码。 7. 指针:指针是一个变量,其值为另一个变量的地址,在C语言中可以通过指针来直接访问和操作内存中的数据。 8. 结构体:结构体是一种自定义的数据类型,可以用来存储不同类型的数据成员。 9. 文件操作:C语言提供了文件操作的函数,可以读取和写入文件中的数据。 10. 预处理器:C语言中的预处理器指令可以在编译之前对代码进行一些处理,如宏定义、条件编译等。 这些是C语言基础知识的一些重要点,掌握这些知识可以帮助你理解和编写C语言程序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值