It looks like your system/toolchain defaults to an unsigned char
type. That means when getopt()
returns -1, it gets converted to 255 and stored in t
. Then that 255 gets promoted to int
type (staying 255) and compared to -1
, which can't ever match.
getopt()
returns int
, so you should really declare t
as int
to match, but if you're set on using char
, you're going to need to use signed char
.
Aside: Since you say you're compiling with gcc, you might also find the -fsigned-char
flag helpful if you want this and other char
variables in your program to be signed.
Second Aside: You can duplicate the failure by passing the -funsigned-char
flag or by changing t
to be an unsigned char
in your Windows test, if that makes it easier to debug.