【413】C 语言 Command line
2020-12-13 03:24
标签:include rev div exec convert lan parameter main pch All the executable programs above have a main(void) program For example: means that: argc refers to the number of arguments, including the program name argc = 3 argv[] allows access to arguments represented as strings argv[0] is a pointer to the string "./a.out" argv[1] is a pointer to the string "-pre" argv[2] is a pointer to the string "3" Write a program that: notice that atoi() had to be called to convert the character to a number Write a program that: takes an optional command-line switch -reverse 【413】C 语言 Command line 标签:include rev div exec convert lan parameter main pch 原文地址:https://www.cnblogs.com/alex-bn-lee/p/11074529.htmlCommand-Line Arguments
1 int main(int argc, char *argv[])
prompt$ ./a.out -pre 3
Command-line argument processing example 1
// commarg.c
#include stdio.h>
#include stdlib.h>
int main(int argc, char *argv[]) {
if (argc == 1 ||
(argc == 2 && atoi(argv[1]) >= 1 && atoi(argv[1]) 4)) {
// we can do something here
} else {
printf("Usage: %s [1|2|3|4]\n", argv[0]);
}
return EXIT_SUCCESS;
}
prompt$ dcc commarg.c
prompt$ ./a.out
prompt$ ./a.out 1
prompt$ ./a.out 2
prompt$ ./a.out 3
prompt$ ./a.out 4
prompt$ ./a.out 5
Usage: ./a.out [1|2|3|4]
Command-line argument processing example 2
// commargrev.c
#include stdio.h>
#include stdlib.h>
#include string.h>
int main(int argc, char *argv[]) {
if (argc == 1 ||
(argc == 2 && !strcmp(argv[1], "-reverse"))) {
// NOTE: strcmp returns 0 if matches.
// we could do something here
} else {
printf("Usage: %s [-reverse]\n", argv[0]);
}
return EXIT_SUCCESS;
}
prompt$ ./a.out
prompt$ ./a.out -reverse
prompt$ ./a.out rubbish
Usage: ./a.out [-reverse]
Makefile