Skip to content

C 标准库

C 标准库(C Standard Library)

头文件功能简介
stdio.h标准输入输出库,包含 printf、scanf、fgets、fputs 等函数。
stdlib.h标准库函数,包含内存分配、程序控制、转换函数等,如 malloc、free、exit、atoi、rand 等。
string.h字符串操作函数,如 strlen、strcpy、strcat、strcmp 等。
math.h数学函数库,包含各种数学运算函数,如 sin、cos、tan、exp、log、sqrt 等。
time.h时间和日期函数,如 time、clock、difftime、strftime 等。
ctype.h字符处理函数,如 isalpha、isdigit、isspace、toupper、tolower 等。
limits.h定义各种类型的限制值,如 INT_MAX、CHAR_MIN、LONG_MAX 等。
float.h定义浮点类型的限制值,如 FLT_MAX、DBL_MIN 等。
assert.h包含宏 assert,用于在调试时进行断言检查。
errno.h定义了错误码变量 errno 及相关宏,用于表示和处理错误。
stddef.h定义了一些通用类型和宏,如 size_t、ptrdiff_t、NULL 等。
signal.h定义了处理信号的函数和宏,如 signal、raise 等。
setjmp.h提供非本地跳转功能的宏和函数,如 setjmp、longjmp 等。
locale.h定义了与地域化相关的函数和宏,如 setlocale、localeconv 等。
stdarg.h提供处理可变参数函数的宏,如 va_start、va_arg、va_end 等。
stdbool.h定义布尔类型和值 true 和 false。
stdint.h定义了精确宽度的整数类型,如 int8_t、uint16_t 等。
inttypes.h提供与整数类型相关的格式化输出宏和函数。
complex.h提供复数运算的函数和宏,如 cabs、carg 等。
tgmath.h为泛型数学函数提供宏,以简化对不同类型数据的数学运算。
fenv.h提供对浮点环境的控制,如舍入模式和异常状态
pthread.h多线程
unistd.hUnix 标准头文件,提供了对 POSIX 操作系统 API 的访问

进程通信

进程方式适用场景数据特点同步要求
管道(Pipe)父子进程字节流阻塞读写
FIFO任意进程字节流可非阻塞
共享内存高性能数据共享直接内存访问需同步
信号进程控制无数据异步
信号量进程控制
消息队列任意进程结构化消息异步

Socket网络编程

常用代码

日志 logger

开启 Mac Coredump

shell
# 开启 Mac Coredump
sudo sysctl kern.coredump=1

# 设置 core 文件大小
ulimit -c unlimited

# 调试 core 文件
lldb -c /cores/core.37676

C 语言标准

查看当前支持的 C 语言标准

shell
$ gcc -E -dM - </dev/null | grep "STDC_VERSION"
#define __STDC_VERSION__ 201710L
结果默认支持标准
#define __STDC_VERSION__ 199901LC99 标准
#define __STDC_VERSION__ 201112LC11 标准
#define __STDC_VERSION__ 201710LC17 标准
-C89 标准

指定 C 语言标准编译

shell
 -std=c11        # Conform to the ISO 2011 C standard
 -std=c89        # Conform to the ISO 1990 C standard
 -std=c90        # Conform to the ISO 1990 C standard
 -std=c99        # Conform to the ISO 1999 C standard

 -std=gnu11      # Conform to the ISO 2011 C standard with GNU extensions(默认)
 -std=gnu89      # Conform to the ISO 1990 C standard with GNU extensions
 -std=gnu90      # Conform to the ISO 1990 C standard with GNU extensions
 -std=gnu99      # Conform to the ISO 1999 C standard with GNU extensions

示例

shell
$ gcc main.c -o test -std=c99

参考

Linux GCC 如何查看及指定 C 语言标准 https://zhuanlan.zhihu.com/p/626785086

嵌入式Linux进程间通信(IPC) https://mp.weixin.qq.com/s/eFNEHQiLp2mZ6ulr7uOHrQ