理解 getopt() 示例。 int 与 char 的比较

发布于 2024-10-19 10:04:21 字数 890 浏览 5 评论 0原文

大家好,我希望你能帮助我理解为什么 getopt 使用 int 以及 getopt 中 optopt 变量的处理。对于 C++ 来说还很陌生。

查看getopt,optopt被定义为一个整数。 http://www.gnu.org/software /libtool/manual/libc/Using-Getopt.html#Using-Getopt

和这里的示例, http://www.gnu .org/software/libtool/manual/libc/Example-of-Getopt.html#Example-of-Getopt

在此示例中,我不明白的部分是如何将整数“c”与整数进行比较switch 语句中的 char。

据我了解,geopt 正在工作的主要参数是字符数组 argv,因此它返回一个 int 的事实对我来说似乎很奇怪,我的期望将是一个 char 并且我需要将任何数字参数转换为 int。字符是否会自动转换为 ANSI 代码并再次转换回来或其他什么? printf 语句

fprintf (stderr, "Unknown option `-%c'.\n", optopt);

按照我的理解期待一个 char,但得到一个 int。为什么 getopt 在处理字符数组时要使用 int ?

我是否遗漏了一些非常明显的东西?我一定是。

Hello all I hope you can help me understand why getopt used an int and the handling of the optopt variable in getopt. Pretty new to C++.

Looking at getopt, optopt is defined as an integer.
http://www.gnu.org/software/libtool/manual/libc/Using-Getopt.html#Using-Getopt

and the example here,
http://www.gnu.org/software/libtool/manual/libc/Example-of-Getopt.html#Example-of-Getopt

In this example the part I do not understand is how `c', an integer is being compared to a char in the switch statement.

As I understand it the main argument geopt is working though is the character array argv so that fact that it deals returns an int seems strange to me, my expectation would be an char and that I would be required to cast any numerical arguments to int. Is the char being automatically converted to it's ANSI code and back again or something? The printf statment

fprintf (stderr, "Unknown option `-%c'.\n", optopt);

Is expecting a char as I understand it, but being given an int. Why would getopt use int when it's dealing with a character array?

Am I missing something really obvious? I must be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-10-26 10:04:21

当整数与字符常量进行比较时,如 optopt == 'c' 所示,字符常量实际上具有 int 类型。事实上,在 C 中,charshort 是二等公民,并且在表达式中总是被提升为 int,因此传递或返回是没有用的一个字符;无论如何,它都会被提升为int

这与 C++ 规则不同,但在 C++ 程序中使用 C 函数(例如 getoptprintf)时应该注意这一点。

事实上,getopt返回一个int还有一个额外的原因:它可能返回一个有效的char值(通常是0..255) -1。如果您想从 getopt 中获取 char,则必须首先检查 -1 的可能性:

int i = getopt(argc, argv, option_string);
if (i == -1)
    // no more options
else {
    char c = i;
    // got a valid char, proceed
}

getopt 返回一个 char,那么就无法区分可能有效的 (char)(-1)(-1 或 255)与停止条件。请参阅此页面,以获取 EOF 值,非常相似。

When an integer is compared to a character constant as in optopt == 'c', the character constant actually has type int. In fact, in C char and short are second-class citizens and are always promoted to int in expressions, so it's no use passing or returning a char; it's promoted to int anyway.

This is different from the C++ rules, but something you should be aware of when using C functions such as getopt and printf in a C++ program.

The fact that getopt returns an int has an additional reason: it may return either a valid char value (typically 0..255) or -1. If you want to get a char out of getopt, you have to check for the -1 possibility first:

int i = getopt(argc, argv, option_string);
if (i == -1)
    // no more options
else {
    char c = i;
    // got a valid char, proceed
}

Had getopt returned a char, then there would be no way to distinguish the possibly valid (char)(-1) (either -1 or 255) from the stop condition. See this page for a better explanation in the context of the EOF value, which is very similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文