#include #include #include int globalInt = 160; void usage(char *cmd) { error(1, 0, "Usage: %s integer\n", cmd); } int main(int argc, char *argv[]) { if ( argc != 2 ) usage(argv[0]); // convert the command line arg from a string (in hex) to a long int int argVal; int retcode = sscanf(argv[1], "%d", &argVal); // scanf needs places to return values... printf("sscanf returned %d\n", retcode); if ( retcode != 1 ) usage(argv[0]); // sscanf returns the number of conversions performed printf("sscanf read %d\n", argVal); // read from stdin until we get either 0 or an error char *str = NULL; int val = 1; do { printf(" please: "); retcode = scanf("%d %ms", &val, &str); printf("scanf returned %d\n", retcode); if ( retcode == 2 ) { printf("scanf got %d '%s'\n", val, str); free(str); str = NULL; } else { fprintf( stderr, "Bad input!\n"); // printing to stderr here break; } } while ( val != 0 ); return EXIT_SUCCESS; }