简体   繁体   中英

The return of getopt in c language

I am so confused about the return of getopt. When does it return (-1)?

When I assign it to an int variable, it returns (-1) if I don't write any options in the terminal window. Whereas it returns the first option character in ascii even if I write more than one option.

But when using it without assigning, it returns (-1) if I write more than one option.

And concerning the attached images, how did it type the error message although the value equals -1?

The Statments

The results

In your example with 2 arguments getopt will return a different value each time you will call it. 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

It should be handled within a loop like this https://www.tutorialspoint.com/getopt-function-in-c-to-parse-command-line-arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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