PDA

View Full Version : C getopt



matmatmat
May 19th, 2009, 04:44 PM
I'm trying to make a simple todo list program/thingy, I am using getopt to recognise options (eg '-a hello' where hello is the option (optarg?):


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]){
FILE *f;
int optchar;
while (optchar = getopt(argc, argv, "a:") != -1){
switch (optchar){
case 'a':
printf(optarg);
printf("case 'a'\n");
break;

}
if (optchar == "a"){
printf("if \"a\"");

}
if (optchar == 'a'){
printf("if 'a'");
}
}
}

when run nothing is outputted?

iiska
May 19th, 2009, 05:13 PM
While condition gets evaluated in wrong order and value 1 is stored to optchar always. Try to add a couple parenthesis to it like this:



while ( (optchar = getopt(argc, argv, "a:")) != -1){

matmatmat
May 19th, 2009, 05:29 PM
Thanks