Minix中_prototype宏

本文探讨了在不同C编译器环境下(K&R与ANSI标准)使用_PROTOTYPE宏来适配函数原型的方法。通过宏定义,可以在K&R风格编译器中忽略参数,而在ANSI C编译器中保留参数,从而实现代码的跨编译器兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Because as the header block says, they didn't know if an ANSI compiler or K&R was going to be used, and this marco allows them to keep the parameters in an ANSI build, and throw them away in a K&R build.

00033   /* Keep everything for ANSI prototypes. */
00034   #define _PROTOTYPE(function, params)    function params

verse

00045   /* Throw away the parameters for K&R prototypes. */
00046   #define _PROTOTYPE(function, params)    function()

which means

00483   _PROTOTYPE( void _exit, (int _status)    );

becomes under ANSI:

void _exit(int _status);

and under K&R:

void _exit();

 

 

The source code you linked to explains it:

00009 * If _ANSI ends up being defined, a macro
00010 *
00011 * _PROTOTYPE(function, params)
00012 *
00013 * is defined. This macro expands in different ways, generating either
00014 * ANSI Standard C prototypes or old-style K&R (Kernighan & Ritchie)
00015 * prototypes, as needed. Finally, some programs use _CONST, _VOIDSTAR etc
00016 * in such a way that they are portable over both ANSI and K&R compilers.
00017 * The appropriate macros are defined here.

Old-style K&R prototypes have the argument names first, then the types:

int foobar (x, y)
   
int x;
   
float *y;
{
   
/* code */
}

ANSI standard prototypes combine them both at the beginning:

int foobar (int x, float *y) {
   
/* code */
}

The _PROTOTYPE macro creates an appropriate signature of either type depending on whether or not _ANSI is defined. In this specific case, K&R signatures are used for function implementations, but the function declarations either include or omit their arguments depending on whether _ANSI is defined.

It is worth noting that K&R-style declarations date from 1978 and most C code currently available will use modern ANSI style signatures. It is rare that you need to support both.

转载于:https://www.cnblogs.com/zdl110110/articles/1888641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值