I have a question about the getopt function, as the following code shows, the type of "ch" is "int",but in the switch clause, it is regarded as "char" .. I am confused why?
Thansk!!
int main(int argc, char **argv)
{
extern int optind;
extern char * optarg;
int ch;
char * format = "f:hnBm:";
// Default makefile name will be Makefile
char szMakefile[64] = "Makefile";
char szTarget[64];
char szLog[64];
while((ch = getopt(argc, argv, format)) != -1)
{
switch(ch)
{
case 'f':
strcpy(szMakefile, strdup(optarg));
break;
case 'n':
break;
case 'B':
break;
case 'm':
strcpy(szLog, strdup(optarg));
break;
case 'h':
default:
show_error_message(argv[0]);
exit(1);
}
}
In C, a char
is really just an integer of a certain size and an int
can be implicitly converted into one, so it works transparently.