Skip to content

Instantly share code, notes, and snippets.

@rsnemmen
Created June 9, 2017 03:36
Show Gist options
  • Select an option

  • Save rsnemmen/aeda00709e267fbcbdd0d66b5851f86c to your computer and use it in GitHub Desktop.

Select an option

Save rsnemmen/aeda00709e267fbcbdd0d66b5851f86c to your computer and use it in GitHub Desktop.
Illustrates how to read command-line arguments in C. Prints usage info if not provided with any argument.
/*
Illustrates how to get arguments from the command-line.
Usage:
args 10.0
will print out the number you entered. If not given any
argument, will print out usage info.
*/
#include <stdio.h>
int main(int argc, char *argv[]){
float arg;
if ( argc != 2 ) {
printf( "usage: %s float \n", argv[0] );
}
else {
sscanf(argv[1], "%f", &arg); // reads command-line argument
printf("Command-line argument: %f \n", arg);
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment