- 成员是数组,可以访问某个索引的偏移
- 使用offset的意义在于,编译器对于成员会自己重排保证对齐
- 所以offset不一定等于你自己手算的
// @zolo // $ ./01.exe // 0 // 4 // 8 // 12 #includeEX-1-系统头文件位置;非标准头,以及用途 系统头文件位置#include struct s { char a; int b[2]; float c; }; int main(void) { printf("%ldn", offsetof(struct s, a)); printf("%ldn", offsetof(struct s, b[0])); printf("%ldn", offsetof(struct s, b[1])); printf("%ldn", offsetof(struct s, c)); }
搜了stdio.h
- windows sdk
- visual studio
- mingw
| aio.h | Deals with asynchronous Input and Output. |
| aliases.h | Functions to access the aliases database that contains mail aliases. |
| alloca.h | Contains function that allocates memory and automatically frees it. |
| argp.h | Handles parsing unix-style program arguments. |
| argz.h | Vector handling an array of strings (program arguments) and a length. |
| ar.h | Describes the structure of files produced by the ‘ar’ program. |
| autosprintf.h | Formatted output to an outputstream. |
| byteswap.h | Macros to swap the order of bytes in integer values. |
| cpio.h | Handles cpio archive format. |
| crypt.h | Password and data encryption. |
| dialog.h | Common declarations for all dialog modules. |
| dirent.h | Handles directory structure/traversal. |
| dlfcn.h | Handles run-time dynamic loading. |
| dlg_colors.h | Color attribute definitions. |
| dlg_config.h | Set of definitions generated by configure. |
| dlg_keys.h | Handles run-time binding support for dialog. |
| elf.h | Defines the format of executable and linking format (ELF) files. |
| endian.h | Handles conversion of integer values to/from Big/Little endian byte ordering. |
| envz.h | Argz vectors with additional constraints. |
| err.h | Utility functions for error messages. |
| error.h | Declaration for error-reporting function. |
| execinfo.h | Handles whether one error is printed per line. |
| fcntl.h | Manipulate file descriptor. |
| fmtmsg.h | Message display handling. |
| fnmatch.h | File/pathname match handling. |
| fstab.h | Defines fstab struct handling internal representation of files. |
| fts.h | Handles traversal of a file hierarchy. |
| ftw.h | Handles walk through a directory tree. |
| _G_config.h | Defines various configuration parameters. |
| gconv.h | Assists in writing additional gconv modules. |
| getopt.h | Parsing command line arguments. |
| gettext-po.h | Processing of PO files. |
| glob.h | Defines structures and symbolic constants used by the glob() function. |
| gnu-versions.h Defines GNU version macros. | |
| grp.h | Declares the group database access structure. |
| gshadow.h | Declares types and functions for shadow group suite. |
| iconv.h | Facilitates character encoding conversion for certain applications. |
| ifaddrs.h | Handles retrieval of networking interface addresses. |
| langinfo.h | Contains language information constants. |
| lastlog.h | Defines the struct lastlog in 4.3BSD |
| libgen.h | Definitions for pattern matching functions. |
| libintl.h | Message catalogs for internationalization. |
| link.h | Data structure for communication from the run-time dynamic linker for loaded ELF shared objects. |
| malloc.h | Prototypes and definition for malloc implementation. |
| mcheck.h | Define debugging hooks for malloc. |
| memory.h | Management of dynamic memory. |
| mntent.h | Utilities for reading/writing fstab, mtab, etc. |
| monetary.h | Monetary value formatting functions. |
| mqueue.h | Defines type for message queue descriptors. |
| netdb.h | Definitions for network database operations. |
| nl_types.h | Defines various data types. |
| nss.h | Handles NSS lookups. |
| obstack.h | Defines object stack macros. |
| paths.h | Defines PATH macros. |
| poll.h | Definitions for the poll function. |
| printf.h | Printf handler extension. |
| pthread.h | Handles threads. |
| pty.h | Declares the functions openpty and forkpty. |
| pwd.h | Defines a password structure. |
| re_comp.h | Defines regular expression matching functions. |
| regex.h | Definitions for data structures and routines for the regular expressions library. |
| regexp.h | Regular expression declarations. |
| resolv.h | Defines types for queries. |
| sched.h | Execution scheduling. |
| search.h | Declarations for System V style searcing functions. |
| semaphore.h | Defines type for performed semaphore operations. |
| shadow.h | Declarations of types and functions for shadow password suite. |
| spawn.h | Definitions for POSIX spawn interface. |
| stab.h | Indicate the GNU stab.h is in use |
| stdc-predef.h | Defines macros for the entirety of the translation unit. |
| stdio_ext.h | Defines functions that access FILE structureinternals. |
| strings.h | Defines non-standard string functions. |
| stropts.h | Streams interface. |
| sudo_plugin.h | Sudo Plugin interface. |
| syscall.h | Linux syscall interface. |
| sysexits.h | Defines exit codes for programs. |
| syslog.h | Definitions for system error logging. |
| tar.h | Extended tar format from POSIX.1 |
| termios.h | Defines values for terminal I/O interfaces. |
| thread_db.h | Interface for debugging. |
| ttyent.h | Defines tty terminal types. |
| ucontext.h | Defines user context types. |
| ulimit.h | Defines constants for ulimit function. |
| unistd.h | Defines miscellaneous symbolic constants and types. |
| utime.h | Set file access and modification times. |
| utmp.h | Information about user currently using system. |
| utmpx.h | User accounting database definitions. |
| values.h | Old compatibility names for limits.h and float.h constants. |
| wordexp.h | Defines word expansion types. |
| xlocale.h | Definition of locale datatype. |
宏隐藏函数指的是,头提供的api函数是宏函数assert,宏assert内部调用了__assert(你可以调用,但是不应该调用)
使用宏隐藏时,函数当然必须在宏前声明
- 因为宏调用了函数
- 如果反过来,assert会展开__assert
# define assert(expr)
((expr)
? __ASSERT_VOID_CAST (0)
: __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
extern void __assert (const char *__assertion, const char *__file, int __line)
__THROW __attribute__ ((__noreturn__));
EX-4-C99标准中为未来库预留的标识符(“future library directions”)
大致意思就是,这些标识符你不要用作自己的标识符
complex.h
Function names:
cerf
cerfc
cexp2
cexpm1
clog10
clog1p
clog2
clgamma
ctgamma
And the same names suffixed with f or l.
ctype.h
Function names:
Any names that begin with is or to, and a lowercase letter may be added
to the declarations in the ctype.h header.
errno.h
Macro names:
Names that begin with E and a digit, or E and an uppercase letter.
inttypes.h
Macro names:
Names that begin with PRI or SCN followed by any lowercase letter.
X may be added to any macros defined in the inttypes.h header.
locale.h
Macro names:
Names that begin with LC_ and an uppercase letter.
signal.h
Macro names:
Names that begin with either SIG and an uppercase letter or SIG_ and an
uppercase letter.
stdint.h
Typedef names:
Names beginning with int or unit and ending with _t.
Macro names:
Names begging with INT or UINT and ending with _MAX, _MIN, or _C.
stdio.h
Lowercase letters may be added to the conversion specifiers and length
modifiers in fprintf and fscanf. Other characters may be used in extensions.
stdlib.h
Function names:
Names that begin with str and a lowercase letter.
string.h
Function names:
Names that begin with str, mem, or wcs and a lowercase letter.
wchar.h
Function names:
Names that begin with wcs and a lowecase letter.
Lowercase letters may be added to the conversion specifiers and length
modifiers in fwprintf and fwscanf. Other characters may be used in
extensions.
wctype.h
Function names:
Names that begin with is or to and a lowercase letter.
EX-5-下面的islower宏为什么不符合标准?
#define islower(c) ((c) >= 'a' && (c) <= 'z')
标准
- 谨慎全面地使用括号保护好
- 每个参数仅被求值一次
这种写法,参数c出现了两次
因此如果c是一个有副作用的表达式,表达式会引起两次副作用
比如islower(c++)
EX-6-- C标准声明:一个文件include了标准头后,不允许重用里声明的宏
- 因为你会修改掉这个宏
- 你自己的项目,宏要带项目前缀,避免冲突
- 下划线打头的,然后是大写字母的宏,被用作标准库内部保留用
- 标准库使用下划线来避免冲突,这样你就可以定义无下划线的宏自己用
包含一个静态数组_ctype,有什么用? - _ctype是把ascii码映射到它的一些flag(用bit-field)
- 比如是否是数字(isdigit),是否是字母(isalphabetic)等等
- 为什么要用数组实现这些宏?
- 如前所说,一般的UPPER实现会对c求值两次
- 使用查表法不需要
- 而且更加快
- 我看了一下源码
- 这些bool信息被手工写入表格
- 访问的方法就是索引字符,然后用掩码取出字段
- 日期:time
- 字符是否是数字:ctype
- int上限:kunuts
- 向上取整浮点数:math
- 一个字符有多少位?:limits
- 一个浮点数的有效位个数:float
- 在字符串中查找特定字符:string
- 打开文件:stdio



