include
#include <stdio.h>
int main() {
printf("Hello, World!你好\n");
return 0;
}
// #include
// <> 寻找系统的资源
// "" 寻找自己写的资源
// .h .hpp 声明文件,头文件
// .c .cpp 实现文件
// int main() 是主函数,程序从这里开始执行
// return 0; 终止 main() 函数,并返回值 0
基本数据类型
#include <stdio.h>
int main() {
printf("Hello, World!你好\n");
int i = 100;
double d = 200;
float f = 300;
long l = 400;
short s = 500;
char c = 'd';
char * str = "hello";
printf("i value : %d\n", i);
printf("d value : %lf\n", d);
printf("f value : %f\n", f);
printf("l value : %d\n", l);
printf("s value : %d\n", s);
printf("c value : %c\n", c);
printf("str value: %s\n", str);
return 0;
}
// 运行结果:
//Hello, World!你好
//i value : 100
//d value : 200.000000
//f value : 300.000000
//l value : 400
//s value : 500
//c value : d
//str value: hello
sizeof 获取类型的存储字节大小
#include <stdio.h>
int main() {
printf("int 占的字节数: %d\n", sizeof(int));
printf("double 占的字节数: %d\n", sizeof(double));
printf("float 占的字节数: %d\n", sizeof(float));
printf("long 占的字节数: %d\n", sizeof(long));
printf("short 占的字节数: %d\n", sizeof(short));
printf("char 占的字节数: %d\n", sizeof(char));
return 0;
}
//int 占的字节数: 4
//double 占的字节数: 8
//float 占的字节数: 4
//long 占的字节数: 4
//short 占的字节数: 2
//char 占的字节数: 1
#include <stdio.h>
int main() {
int arrInt[3] ;
double arrDouble[3] ;
printf("%d\n", sizeof(arrInt)); // 12 == 3 * 4 int的字节数为4,数组数量为3故乘3
printf("%d\n", sizeof(arrDouble)); // 24 == 3 * 8 double字节数为8
return 0;
}
指针
指针是内存地址,指针变量用来存放内存地址的变量。
%p 地址输出的占位
& 取出地址
指针占用的内存大小为:4个字节(32位)。
#include <stdio.h>
int main() {
int i = 100;
printf("i value: %d\n", i);
/*
* 指针取值
*/
// 指针变量声明
int *ip;
ip = &i; // &i,取出 变量i 的地址
printf("变量地址为:%p\n", ip);
printf("变量的值为:%d\n", *ip);
return 0;
}
//i value: 100
//变量地址为:004FFA88
//变量的值为:100
通过指针修改值
#include <stdio.h>
int main() {
int i = 100;
int * ip = &i;
* ip = 300;
printf("i value : %d\n", i); // i value : 300
return 0;
}
函数方式去操作修改值
#include <stdio.h>
void change(int i){
i = 200;
}
void change2(int *i){
* i = 300; // 指针操作修改
}
int main() {
int i = 100;
change(i);
printf("i value : %d\n", i); // i value : 100
change2(&i);
printf("i value : %d\n", i); // i value : 300
return 0;
}
函数方式,互换值
#include <stdio.h>
void change(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a = 100;
int b = 200;
change(&a, &b);
printf("a value : %d\n", a); // a value : 200
printf("b value : %d\n", b); // b value : 100
return 0;
}
二级指针
指针存放的是内存地址;自己也有内存地址。
一级指针:*
二级指针:**
三级指针:***
#include <stdio.h>
int main() {
int a = 100;
int * a_p = &a;
int ** a_p_p = &a_p;
printf("a_p的值为: %p, a_p_p的值为:%p\n", a_p, a_p_p);
//a_p的值为: 0073FE98, a_p_p的值为:0073FE8C
printf("获取最终的值为:%d\n", **a_p_p);
//获取最终的值为:100
return 0;
}
数组与数组指针
#include <stdio.h>
int main() {
int arr[] = {1,2,3,4};
// 错误方式
// for (int i = 0; i < 4; ++i) {
// }
// 正确的方式
int i = 0;
for (i = 0; i < 4; i++) {
printf("arr is %d\n", arr[i]);
}
return 0;
}
数组的内存地址 == 第一个元素的内存地址
#include <stdio.h>
int main() {
int arr[] = {1,2,3,4};
printf("arr is %p\n", arr); // arr is 00F3FB3C
printf("arr is %p\n", &arr); // arr is 00F3FB3C
printf("arr is %p\n", &arr[0]); // arr is 00F3FB3C
return 0;
}
采用指针遍历数组 - 指针挪动
#include <stdio.h>
int main() {
int arr[] = {1,2,3,4};
int * arr_p = arr;
int i;
for (i = 0; i < 4; i++) {
// // 方式1
// printf("arr_p is %d\n", arr_p[i]);
//
// // 方式2
// printf("arr_p is %d\n", *(arr_p + i));
// 方式3
printf("arr_p is %d\n", *arr_p);
arr_p ++;
}
return 0;
}
循环给数组赋值
#include <stdio.h>
int main() {
int arr[4] ;
int * arr_p = arr;
// 赋值
int i;
for (i = 0; i < 4; i++) {
*(arr_p + i) = 100 + i;
}
// 遍历
i = 0;
for (i = 0; i < 4; i++) {
printf("arr_p is %d\n", *(arr_p + i));
}
return 0;
}
函数指针
#include <stdio.h>
void add(int num1, int num2){
printf("num1 + num2 = %d \n", (num1 + num2));
}
void mins(int num1, int num2){
printf("num1 - num2 = %d \n", (num1 - num2));
}
// 回调到 add 和 mins方法
// void(*method)(int, int) 声明 函数指针
// void为返回值
// (*method)函数名
// (int, int)参数
void operate(void(*method)(int, int), int num1, int num2){
method(num1, num2);
printf("operate函数的 method指针是:%p\n", method);
}
int main() {
operate(add, 100, 10);
operate(mins, 100, 10);
printf("main函数的 add指针是:%p\n", add);
printf("main函数的 mins指针是:%p\n", mins);
return 0;
}
//num1 + num2 = 110
//operate函数的 method指针是:0037123F
//num1 - num2 = 90
//operate函数的 method指针是:0037123A
//main函数的 add指针是:0037123F
//main函数的 mins指针是:0037123A
回调
#include <stdio.h>
void callBackMethod(char * fileName, int current, int total){
printf("%s process:%d/%d\n", fileName, current, total);
}
void compress(char * fileName, void(*callBackP)(char *, int, int)){
callBackP(fileName, 30, 100);
}
int main() {
// 方式1
// void(*call)(char *, int, int) = callBackMethod;
// 方式2
void(*call)(char *, int, int);
call = callBackMethod;
compress("a.jpg", call);
return 0;
}
// result:
// a.jpg process:30/100
字符串 char *
a)定义字符串的两种方式
#include <stdio.h>
int main() {
// 方式1
// \0 给printf用的,遇到 \0 才结束
char str[] = {'a', 'b', 'c', 'd', 'e', '\0'};
str[2] = 'p';
printf("第一种方式:%s\n", str); // 第一种方式:abpde
// 方式2
char * str2 = "abcde";
// str2[2] = 'z'; // 这里会崩溃掉,不允许这么操作
printf("第二种方式:%s\n", str2); // 第二种方式:abcde
return 0;
}
b)获取字符串长度 - 方式1
#include <stdio.h>
void getLenOfChar(int * lenResult, char * string){
int count = 0;
while (*string){ // 不为 \0 一直循环
string++;
count ++;
}
*lenResult = count;
}
int main() {
// \0 给printf用的,遇到 \0 才结束
// char str[] = {'a', 'b', 'c', 'd', 'e', '\0'};
char * str = "abcde";
int lenValue = 0;
getLenOfChar(&lenValue, str);
printf("长度为:%d\n", lenValue); // 5
return 0;
}
c)获取字符串长度 - 方式2
#include <stdio.h>
#include <string.h>
int main() {
char * str = "abcde";
// 方式2
int lenValue = strlen(str);
printf("长度为:%d\n", lenValue); // 5
return 0;
}
d)字符串的转换 - 字符串转成int 和 double
#include <stdio.h>
#include <stdlib.h>
int main() {
char * str = "23.91";
int result = atoi(str);
if (result){ // 非零 即true
printf("转换后:%d\n", result); // 转换后:
}
double d = atof(str);
printf("value:%lf\n", d); // 23.910000
return 0;
}
e)字符串的比较 - strcmp/strcmpi
strcmp 区分大小写
strcmpi 不区分大小写
#include <stdio.h>
#include <string.h>
int main() {
char *str1 = "hello";
char *str2 = "Hello";
int result = strcmp(str1, str2); // 区分大小写
result = strcmpi(str1, str2); // 不区分大小写
if (result) { // 0 相同,非0不同
printf("不相同的 %d\n", result); // 不相同的
} else {
printf("相同的 %d\n", result);
}
return 0;
}
f)字符串的查找 - strstr
#include <stdio.h>
#include <string.h>
int main() {
char *str1 = "helloWorldHelloJava";
char * pop = strstr(str1, "H");
if (pop) { // 非NULL查到了
printf("包含了,查到了 %s\n", pop); // 查H HelloJava
} else {
printf("没包含,没查到 %s\n", pop); // 查w (null)
}
// 求索引
int index = pop - str1;
printf("索引为:%d\n", index);
// 查w 索引为:-6918216
// 查H 索引为:10
return 0;
}
g)字符串的拼接 - strcpy/strcat
#include <stdio.h>
#include <string.h>
int main() {
char *str1 = "helloWorld";
char *str2 = "helloJava";
char dest[25]; // 定义一个容器
strcpy(dest, str1); // 拷贝
strcat(dest, str2); // 拼接
printf("拼接后:%s\n", dest); // 拼接后:helloWorldhelloJava
return 0;
}
h)字符串的大小写转换 - tolower/toupper
#include <stdio.h>
#include <ctype.h>
void lower(char * dest, char * str){
char * tempStr = str; // 临时指针,避免破坏str
while (*tempStr){
* dest = tolower(*tempStr);
tempStr++;
dest++;
}
*dest = '\0'; // 避免打印系统值,乱码
}
int main() {
char *str1 = "helloWorld";
char dest[35]; // 容器
lower(dest, str1);
printf("转换后:%s\n", dest); // 转换后:helloworld
return 0;
}
i)字符串的截取
#include <stdio.h>
#include <string.h>
void subStringAction1(char * result, char * str, int start, int end){
char * temp = str;
int count = 0;
while (*temp){
if (count >= start && count < end){
*result = *temp;
result++;
}
temp++;
count++;
}
}
void subStringAction2(char *result, char * str, int start, int end){
for (int i = start; i < end; ++i) {
*(result++) = *(str+i);
}
}
void subStringAction3(char *result, char * str, int start, int end){
strncpy(result, str + start, (end - start));
}
int main() {
char * str = "helloWorld";
char * result = "";
// subStringAction1(result, str, 2, 5);
// subStringAction2(result, str, 2, 5);
subStringAction3(result, str, 2, 5);
printf("结果为:%s\n", result);
return 0;
}
int
a)获取int数组的长度
#include <stdio.h>
// 数组作为参数传递,会把数组优化成指针
void getLenOfIntArr(int * intArrLen, int arr[]){
*intArrLen = sizeof(arr) / sizeof(int); // 1
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7};
int len = 0;
len = sizeof(arr) / sizeof(int);
printf("方式1:arr 长度为:%d\n", len); // 7
len = 0;
getLenOfIntArr(&len, arr);
printf("方式2:arr 长度为:%d\n", len); // 1
return 0;
}
结构体
a)第1种写法
#include <stdio.h>
#include <string.h>
struct Dog{
char name[10];
int age;
char sex;
};
int main() {
struct Dog dog;
strcpy(dog.name, "汪汪");
dog.age = 1;
dog.sex = 'm';
printf("输出, name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex);
// 输出, name:汪汪, age:1, sex:m
return 0;
}
b)第2种写法
#include <stdio.h>
#include <string.h>
struct Dog{
char name[10];
int age;
char sex;
} ddd = {"汪汪", 2, 'm'},
ddd2,
ddd3
;
int main() {
printf("输出, name:%s, age:%d, sex:%c \n", ddd.name, ddd.age, ddd.sex);
// 输出, name:汪汪, age:2, sex:m
printf("输出, name:%s, age:%d, sex:%c \n", ddd2.name, ddd2.age, ddd2.sex);
// 输出, name:, age:0, sex:
strcpy(ddd3.name, "汪汪");
ddd3.age = 3;
ddd3.sex = 'm';
printf("输出, name:%s, age:%d, sex:%c \n", ddd3.name, ddd3.age, ddd3.sex);
// 输出, name:汪汪, age:3, sex:m
return 0;
}
c)第3种写法
#include <stdio.h>
struct Study{
char * studyContent;
};
struct Student {
char name[10];
int age;
char sex;
struct Study study;
struct Play{
char * playContent;
} play;
};
int main() {
struct Student student = {
"张三", 21, 'M',
{"学C"},
{"play basketball"}};
printf("输出, name:%s, age:%d, sex:%c, study:%s, play:%s \n",
student.name,
student.age,
student.sex,
student.study,
student.play);
return 0;
}
结构体指针
#include <stdio.h>
#include <string.h>
struct Student {
char name[10];
int age;
char sex;
};
int main() {
struct Student student = {"张三", 21, 'M'};
printf("输出, name:%s, age:%d, sex:%c \n",student.name, student.age, student.sex);
struct Student * studentP = &student;
studentP->age = 23;
strcpy(studentP->name, "李四");
printf("输出, name:%s, age:%d, sex:%c \n",student.name, student.age, student.sex);
return 0;
}
结构体数组
#include <stdio.h>
struct Student {
char name[10];
int age;
char sex;
};
int main() {
struct Student student[5] = {
{"张1", 21, 'M'},
{"张2", 22, 'M'},
{"张3", 23, 'M'},
{"张4", 24, 'M'},
{"张5", 25, 'M'}
};
printf("输出, name:%s, age:%d, sex:%c \n",student[0].name, student[0].age, student[0].sex);
// 输出, name:张1, age:21, sex:M
struct Student student2 = {"张22222", 22, 'M'};
// student[1] = student2;
* (student + 1) = student2;
printf("输出, name:%s, age:%d, sex:%c \n",student[1].name, student[1].age, student[1].sex);
// 输出, name:张22222, age:22, sex:M
return 0;
}
结构体别名 - 方式1
#include <stdio.h>
#include <string.h>
struct Student {
char name[10];
int age;
char sex;
};
// 给结构体取别名
typedef struct Student Student;
// 给结构体指针取别名
typedef Student * StudentP;
int main() {
Student student = {"张1", 21, 'M'};
printf("输出, name:%s, age:%d, sex:%c \n",student.name, student.age, student.sex);
// 输出, name:张1, age:21, sex:M
StudentP studentP = &student;
studentP->age = 23;
strcpy(studentP->name, "李四");
printf("输出, name:%s, age:%d, sex:%c \n",student.name, student.age, student.sex);
// 输出, name:李四, age:23, sex:M
return 0;
}
结构体别名 - 方式2
#include <stdio.h>
#include <string.h>
// 给结构体取别名
typedef struct {
char name[10];
int age;
char sex;
} Student;
int main() {
Student student = {"张1", 21, 'M'};
printf("输出, name:%s, age:%d, sex:%c \n",student.name, student.age, student.sex);
// 输出, name:张1, age:21, sex:M
Student * studentP = &student;
studentP->age = 23;
strcpy(studentP->name, "李四");
printf("输出, name:%s, age:%d, sex:%c \n",student.name, student.age, student.sex);
// 输出, name:李四, age:23, sex:M
return 0;
}
enum 枚举
#include <stdio.h>
enum CommentType {
TEXT = 10, // 后续枚举成员的值在前一个成员上加 1 即 TEXT 10, TEXT_IMAGE 11, IMAGE 12
TEXT_IMAGE,
IMAGE
};
int main() {
enum CommentType type1 = TEXT;
enum CommentType type2 = TEXT_IMAGE;
enum CommentType type3 = IMAGE;
printf("%d, %d, %d \n", type1, type2, type3); // 10, 11, 12
return 0;
}
枚举别名
#include <stdio.h>
typedef enum {
TEXT = 10, // 后续枚举成员的值在前一个成员上加 1 即 TEXT 10, TEXT_IMAGE 11, IMAGE 12
TEXT_IMAGE,
IMAGE
} CommentType;
int main() {
CommentType type1 = TEXT;
CommentType type2 = TEXT_IMAGE;
CommentType type3 = IMAGE;
printf("%d, %d, %d \n", type1, type2, type3); //
return 0;
}
文件的操作
r:读
a+:读 、追加写
rb 、 rw
读文件
#include <stdlib.h>
#include <stdio.h>
int main(){
FILE * file = fopen("C:\\xxx\\xxxx\\xxx\\test.txt", "r");
if (!file){
printf("打开文件失败,请检查!");
exit(0);
}
// 定义缓存区域
char buffer[10];
// 缓冲区buffer 长度10 文件变量指针
while (fgets(buffer, 10, file)){
printf("%s", buffer);
}
// 关闭文件
fclose(file);
return 0;
}
写文件
#include <stdlib.h>
#include <stdio.h>
int main(){
FILE * file = fopen("C:\\xxx\\xxxx\\xxx\\test1.txt", "a+");
if (!file){
printf("打开文件失败,请检查!");
exit(0);
}
// 写入
fputs("This is xxxxxx \n", file);
// 关闭文件
fclose(file);
return 0;
}
复制文件
#include <stdlib.h>
#include <stdio.h>
int main(){
FILE * srcFile = fopen("C:\\xxx\\xxx\\xxx\\test1.txt", "rb");
FILE * destFile = fopen("C:\\xxx\\xxx\\xxx\\test2.txt", "wb");
if (!srcFile || !destFile){
printf("打开文件失败,请检查!");
exit(0);
}
char buffer[514]; // 514 * 4 = 2048
int len;
// 容器buffer
// 每次偏移多少
// 容器大小 sizeof(buffer) / sizeof(int) 等价于 514
while ((len = fread(buffer, sizeof(int), sizeof(buffer) / sizeof(int), srcFile)) != 0){
fwrite(buffer, sizeof(int), len, destFile);
}
// 关闭文件
fclose(srcFile);
fclose(destFile);
return 0;
}
获取文件大小
#include <stdlib.h>
#include <stdio.h>
int main(){
FILE * srcFile = fopen("C:\\xxx\\xxx\\xxx\\test1.txt", "rb");
if (!srcFile){
printf("打开文件失败,请检查!");
exit(0);
}
fseek(srcFile, 0, SEEK_END);
long file_size = ftell(srcFile);
printf("字节大小size: %ld \n", file_size);
// 关闭文件
fclose(srcFile);
return 0;
}
C函数速查手册 资源
下载地址(微云):https://share.weiyun.com/c1QgcdwS