理解 getopt() 示例。 int 与 char 的比较
大家好,我希望你能帮助我理解为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当整数与字符常量进行比较时,如
optopt == 'c'
所示,字符常量实际上具有int
类型。事实上,在 C 中,char
和short
是二等公民,并且在表达式中总是被提升为int
,因此传递或返回是没有用的一个字符
;无论如何,它都会被提升为int
。这与 C++ 规则不同,但在 C++ 程序中使用 C 函数(例如
getopt
和printf
)时应该注意这一点。事实上,
getopt
返回一个int
还有一个额外的原因:它可能返回一个有效的char
值(通常是0..255) 或 -1。如果您想从getopt
中获取char
,则必须首先检查 -1 的可能性: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 typeint
. In fact, in Cchar
andshort
are second-class citizens and are always promoted toint
in expressions, so it's no use passing or returning achar
; it's promoted toint
anyway.This is different from the C++ rules, but something you should be aware of when using C functions such as
getopt
andprintf
in a C++ program.The fact that
getopt
returns anint
has an additional reason: it may return either a validchar
value (typically 0..255) or -1. If you want to get achar
out ofgetopt
, you have to check for the -1 possibility first:Had
getopt
returned achar
, 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 theEOF
value, which is very similar.