stddef.h头文件学习

stddef.h头文件看意思就是标准定义,定义有一些变量和宏。其中定义了NULL和offsetof()宏,NULL指向0或者是无效指针,offsetof获取一个元素在结构中的偏移量。下面看看具体的代码注释和笔记。

#ifndef _LINUX_STDDEF_H
#define _LINUX_STDDEF_H

#include <linux/compiler.h>

/*
 * #undef 预定义取消指令
 *
 * 如果NULL没有被预定义,则会忽略#undef指令
 * 一旦NULL被预定义了,则就会取消NULL的预定义
 */
#undef NULL
#if defined(__cplusplus)
#define NULL 0       //NULL被赋值为0
#else
#define NULL ((void *)0)   //NULL定义为无效的指针,将0强制转化为(void*)类型的指针
#endif


//如果offsetof被定义了,就会取消该定义
#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
/*
 * 先指出目的:就是求出MEMBER在TYPE中的偏移量
 *
 * 原理:(TYPE *)0 将0强制转化成(TYPE*)类型的指针,记做TYPE *ptr = (TYPE *)0;
 *       ptr->MEMBER就是TYPE中的元素MEMBER,然后&(ptr->MEMBER),取得MEMBER的地址
 *       由于其基地址是0,最后在进行强制类型转化成int类型,就获取了MEMBER在TYPE中
 *       的偏移量了。
 */
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#endif
stdio.h是C语言中的标准输入输出头文件,定义了一些输入输出操作的函数和宏。下面是stdio.h头文件的完整代码: ``` #ifndef _STDIO_H #define _STDIO_H #include <sys/cdefs.h> #include <sys/types.h> #include <stdarg.h> #define __need_size_t #define __need_NULL #include <stddef.h> #define __need_FILE #define __need___FILE #include <bits/types/FILE.h> #define BUFSIZ 8192 #define EOF (-1) #define FOPEN_MAX 20 #define FILENAME_MAX 1024 #define L_tmpnam 20 #define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 #define TMP_MAX 26 #define stdin (&__sF) #define stdout (&__sF) #define stderr (&__sF) extern struct __sFILE __sF; int remove(const char *); int rename(const char *, const char *); FILE *tmpfile(void); char *tmpnam(char *); int fclose(FILE *); int fflush(FILE *); FILE *fopen(const char *, const char *); FILE *freopen(const char *, const char *, FILE *); void setbuf(FILE *, char *); int setvbuf(FILE *, char *, int, size_t); int fprintf(FILE *, const char *, ...); int fscanf(FILE *, const char *, ...); int printf(const char *, ...); int scanf(const char *, ...); int snprintf(char *, size_t, const char *, ...); int sscanf(const char *, const char *, ...); int vfprintf(FILE *, const char *, va_list); int vfscanf(FILE *, const char *, va_list); int vprintf(const char *, va_list); int vscanf(const char *, va_list); int vsnprintf(char *, size_t, const char *, va_list); int vsscanf(const char *, const char *, va_list); int fgetc(FILE *); char *fgets(char *, int, FILE *); int fputc(int, FILE *); int fputs(const char *, FILE *); int getc(FILE *); int getchar(void); char *gets(char *); int putc(int, FILE *); int putchar(int); int puts(const char *); int ungetc(int, FILE *); size_t fread(void *, size_t, size_t, FILE *); size_t fwrite(const void *, size_t, size_t, FILE *); int fgetpos(FILE *, fpos_t *); int fseek(FILE *, long int, int); int fsetpos(FILE *, const fpos_t *); long int ftell(FILE *); void rewind(FILE *); void clearerr(FILE *); int feof(FILE *); int ferror(FILE *); void perror(const char *); #endif /* _STDIO_H */ ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天堂1223

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值