当前位置:网站首页>C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
2022-08-01 21:21:00 【weixin_Guest time】
Standard I/O Libraries and C Preprocessors
Some functions not implemented by C compilers must be implemented in other ways.In C, they are processed at runtime, either in application code or in a runtime library.In the C language, the vast majority of library functions or auxiliary programs need to be called explicitly.For example, in C (where necessary), the programmer must manage the use of dynamic memory, create arrays of various sizes, test array bounds, and perform range checking.
C originally did not define I/O, but provided by library functions.Later, this actually became the standard mechanism.
Portable I/O, written by Mike Lesk, first appeared in 1972, optimized and tailored to become the standard I/O library.
C Preprocessor
Initiator: Alan Snyder
Time: 1972
Three main functions implemented:
String replacement.Usually used to give a constant a symbolic name.
The header file contains.(The general declarations are separated into header files and can be used by many source files, although the convention uses a .h extension for header files)
An extension to the generic code template.Unlike functions, macros (marco) can receive arguments of different types in successive invocations (the actual arguments of the macro are just output as-is). In the expansion of a macro, whitespace can make a big difference to the expansion.
// 1.
#Define a (y) a_expanded (y)
a (x);a (y) a_expanded(y)
a(x);
is expanded to
(y) a_expanded(y)(x);
You might think that using curly braces in macros, like in other parts of C, would combine redundant statements into a single compound statement, but that's not the case.Preprocessors like macros should only be used in moderation.
//2.
/*People: Steve Bourne
*Location: Bell Labs
*Shell (command interpreter) for UNIX v7
*Use the preprocessor to make CThe language looks more like Algol-68
*/
#define STRING char *
#define IF if (
#define THEN ) {
#define ELSE } else (
#define FI ;}
#define WHILE while (
#define DO ) {
#define OD ;}
#define INT int
#define BEGIN {
#define END }
INT compare(s1, s2) STRING s1; STRING s2;
BEGIN
WHILE *s1++ == *s2
DO IF *s2++ == 0
THEN return(0)
FI
OD
return (*--s1 - *s2);
END
/*Translation, just replace it as it is*/
int compare(s1, s2) char *s1; char *s2;
{
while (*s1++ == *s2 ) {
if (*s2++ == 0) {
return (0);
}
;}
return (*--s1 - *s2);
} }
Corresponding C code:
int compare(s1, s2)
char *s1, *s2;
{
while (*s1++ == *s2) {
if (*s2 ++ == 0) Return (0);
}
Return (*-s1-*s2);
}
/*Shell does not use malloc, but uses sbrk to manage the heap storage itself, Steve explained,
* *The reason why he uses this special memory allocator is to improve the efficiency of string processing
* */
/*Bource's creation of this variant of C actually led to the whimsical International Obfuscated C Code Competition
* (The International Obfuscated C Code Competition)
*/
Macros are best used only to name constants and provide a shorthand notation for some appropriate construct.Macro names should be capitalized so that
can be easily distinguished from function calls.Never use the C preprocessor to modify the underlying structure of the language, because then C is not C.
边栏推荐
- JS hoisting: how to break the chain of Promise calls
- JS提升:手写发布订阅者模式(小白篇)
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.4 K&R C
- C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
- 记录第一次给开源项目提 PR
- 【微信小程序】【AR】threejs-miniprogram 安装(76/100)
- Jmeter实战 | 同用户重复并发多次抢红包
- C Pitfalls and Defects Chapter 7 Portability Defects 7.6 Memory Location 0
- 2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型
- C语言_枚举类型介绍
猜你喜欢

CS-NP白蛋白包覆壳聚糖纳米颗粒/人血清白蛋白-磷酸钙纳米颗粒无机复合材料

ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!

用户量大,Redis没法缓存响应,数据库宕机?如何排查解决?

Interview Blitz 70: What are sticky packs and half packs?How to deal with it?

LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟

Hiking, cured my mental internal friction

技能大赛训练:A部分加固题目

ORI-GB-NP半乳糖介导冬凌草甲素/姜黄素牛血清白蛋白纳米粒的研究制备方法

方舟:生存进化官服和私服区别

Shell编程之条件语句
随机推荐
织梦发布文章提示body has not allow words错误
Appendix A printf, varargs and stdarg A.1 printf family of functions
那些关于DOM的常见Hook封装(一)
LVS负载均衡群集
JS Improvement: Handwritten Publish Subscriber Model (Xiaobai)
【微信小程序】【AR】threejs-miniprogram 安装(76/100)
测试的意义并不是能找到全部的缺陷
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
[译] 容器和 Kubernetes 中的退出码完整指南
Pytorch学习记录(八):生成对抗网络GAN
PX4模块设计之十五:PX4 Log设计
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
Kubernetes 如何实现组件高可用
51.【结构体初始化的两种方法】
【Jmeter常用断言组件】
一个关于操作数据库的建议—用户密码
Jmeter实战 | 同用户重复并发多次抢红包
LeetCode
软考 ----- UML设计与分析(上)
如何封装 cookie/localStorage/sessionStorage hook?