Created
June 9, 2017 03:36
-
-
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.
This file contains hidden or 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
| /* | |
| 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