Created
July 3, 2017 16:18
-
-
Save afnanenayet/169e2fcc5fa44652e473b224dc3f6b50 to your computer and use it in GitHub Desktop.
Command line arguments activity solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* atoi - read an integer from each argument, | |
* and print them to stdout. catch errors. | |
* | |
* usage: ./atoi [integer]... | |
* | |
* CS50, July 2017 | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(const int argc, const char *argv[]) | |
{ | |
int status = 0; | |
for (int i = 1; i < argc; i++) { | |
int tmp; | |
printf("argument %d: ", i); | |
if (sscanf(argv[i], "%d", &tmp) != 1) { | |
printf("'%s' is not a valid integer\n", argv[i]); | |
} else { | |
printf("%d\n", tmp); | |
} | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment