Skip to content

Instantly share code, notes, and snippets.

@afnanenayet
Created July 3, 2017 16:18
Show Gist options
  • Save afnanenayet/169e2fcc5fa44652e473b224dc3f6b50 to your computer and use it in GitHub Desktop.
Save afnanenayet/169e2fcc5fa44652e473b224dc3f6b50 to your computer and use it in GitHub Desktop.
Command line arguments activity solution
/*
* 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