C 函数中的可选参数 [英] Optional arguments in C function

查看:34
本文介绍了C 函数中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 函数中,我想检查是否存在输入参数(在我的例子中为值").

In a C function, I want to check if an input argument ('value' in my case) is presented or not.

即:

void Console(char string[], int32_t value)
{
    // write string here
    // write value here, if it exists
}

当使用if(value != NULL)语句时,我的Console()函数发送4096

When used if(value != NULL) statement, my Console() function sends 4096

如何根据参数存在进行检查和操作?

How can I check and act based on argument existence?

推荐答案

C 语言中通常不允许可选参数(但它们存在于 C++Ocaml 等..)唯一的例外是可变参数(如printf).

Optional arguments are generally not allowed in C (but they exist in C++ and in Ocaml, etc...). The only exception is variadic functions (like printf). 

过去,来自 POSIX 的 open(2) 函数在某些情况下接受可选的第三个参数(在定义时 - 在 1970 年代和 1980 年代 - 调用约定实际上将参数推送到调用堆栈上,因此忽略该参数很容易实现).如果您今天在 Linux 上的自由软件 libc 实现中查看该 open 函数的最新实现,例如 musl-libc,你可以在它的 src 中看到/fcntl/open.c 它使用 可变参数工具(通常作为编译器内置函数实现).

Historically, the open(2) function from POSIX accepted in some cases an optional third argument (at the time it was defined - in the 1970s and 1980s -, the calling conventions practically pushed arguments on the call stack, so ignoring that argument was simple to implement). If you look today at recent implementation of that open function in free software libc implementations on Linux, such as musl-libc, you see in its src/fcntl/open.c that it uses the <stdarg.h> variadic facilities (which are often implemented as compiler builtins).

顺便说一句,你可以定义一些宏来填充缺失"的参数,所以如果你有

BTW, you could define some macros to fill the "missing" arguments, so if you have

  void console(const char*, int32_t);

你也可能

  #define console_plain(Msg) console((Msg),0)

这可能是某些标题中的一些内联函数,例如

and that could be instead some inline function in some header, e.g.

  static void inline console_plain (const char*msg) 
  { console(msg, 0); }

然后在其他地方使用 console_plain("hello here")

然后你的可变参数函数应该定义如何以及允许什么参数(在一个非空的 fixed 参数序列之后).并使用 stdarg(3) 来获取这些可变参数(实际)参数.

Then your variadic function should define how and what arguments are allowed (after a non-empty sequence of fixed arguments). And use stdarg(3) to get these variadic (actual) arguments.

实际参数主要在编译时知道,而不是在运行时知道.因此,您需要一个约定,该约定通常定义从所需的固定参数中允许使用哪些可变参数.特别是,您无法测试参数是否存在(该信息在运行时丢失).

The actual arguments are known mostly at compile-time, not at run-time. So you need a convention which often defines which variadic argument are permitted from the required fixed arguments. In particular, you have no way to test that an argument is present (that information is lost at runtime).

顺便说一句,对于可变参数函数,您通常会失去大多数 C 编译器提供的类型检查(至少在您启用所有警告时,例如 gcc -Wall -Wextra).如果使用 GCC你可能有一些 函数 __attribute__-s(如 formatsentinel、....)在原型中提供帮助.您甚至可以使用过时的 MELT 或在 2019 年自定义 gcc使用您的 GCC 插件,添加您自己的属性,执行自己的类型检查.

BTW, with variadic functions you generally lose the typechecking that most C compilers provide (at least when you enable all warnings, e.g. gcc -Wall -Wextra). If using GCC you might have some function __attribute__-s (like format, sentinel, ....) in the prototype to assist that. You could even customize gcc with the obsolete MELT, or in 2019 with your GCC plugin, to add your own attributes doing their own type checking.

如何根据参数存在进行检查和操作?

How can I check and act based on argument existence?

使用当前通常的调用约定(例如研究 x86-64 ABI) 你通常不能这样做(不使用可变参数函数).

With current usual calling conventions (e.g. study the x86-64 ABI) you generally cannot do that (without using variadic functions).

这篇关于C 函数中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆