-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-line-arguments.c
More file actions
38 lines (21 loc) · 943 Bytes
/
command-line-arguments.c
File metadata and controls
38 lines (21 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
/*
The below arguments within the main function are what handle the command line arguments.
argc is the number of arguments passed and argv is a pointer array which points to each
argument within the program.
*/
int main(int argc, char *argv[]){
if(argc == 2 ){
//prints back the argument given. note: the argv[1] points to the argument, not the program name
printf("The argument supplied is %s\n", argv[1]);
}else if(argc > 2){
//prints error along with the command usage. the argv[0] is the program name
fprintf(stderr, "Too many arguments! Usage: %s <argument>\n", argv[0]);
return 1;
}else{
//see previous comment
fprintf(stderr, "At least one argument is needed. Usage: %s <argument>\n", argv[0]);
return 1;
}
return 0;
}