简体   繁体   English

c语言中getopt的返回

[英]The return of getopt in c language

I am so confused about the return of getopt.我对 getopt 的回归感到很困惑。 When does it return (-1)?它什么时候返回(-1)?

When I assign it to an int variable, it returns (-1) if I don't write any options in the terminal window.当我将它分配给一个 int 变量时,如果我不在终端 window 中写入任何选项,它会返回 (-1)。 Whereas it returns the first option character in ascii even if I write more than one option.而即使我写了多个选项,它也会返回 ascii 中的第一个选项字符。

But when using it without assigning, it returns (-1) if I write more than one option.但是在不分配的情况下使用它时,如果我写了多个选项,它会返回 (-1)。

And concerning the attached images, how did it type the error message although the value equals -1?关于附加的图像,尽管值等于-1,它是如何输入错误消息的?

The Statments声明

The results结果

In your example with 2 arguments getopt will return a different value each time you will call it.在您使用 2 arguments getopt 的示例中,每次调用它时都会返回不同的值。 The first time it will return 'a' then it will return 'b' and the last time it has no more options to read from and will return -1第一次它会返回 'a' 然后它会返回 'b' 并且最后一次它没有更多的选项可以读取并且会返回 -1

It should be handled within a loop like this https://www.tutorialspoint.com/getopt-function-in-c-to-parse-command-line-arguments它应该在这样的循环中处理https://www.tutorialspoint.com/getopt-function-in-c-to-parse-command-line-arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM