使用教材
《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794
Makefile
CC=clang
CFLAGS=-Wall -g
clean:
rm -f ex8
-
上面的内容复制到本地的时候,如果显示
Makefile:5: *** missing separator. Stop.
,基本上是因为rm -f ex8
这一行前面留了空格键,需要改成 TAB键 (这是严格要求的); -
CC
字段指定使用clang
编译器; -
clang
编译器安装,参见
ex8.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 0;
if(argc == 1) {
printf("You only have one argument. You suck.\n");
} else if(argc > 1 && argc < 4) {
printf("Here's your arguments:\n");
for(i = 0; i < argc;i++) {
printf("%s\n", argv[i]);
}
printf("\n");
} else {
printf("You have too many arguments. You suck.\n");
}
return 0;
}
Run
anno@anno-m:~/Documents/mycode/ex8$ make ex8
clang -Wall -g ex8.c -o ex8
anno@anno-m:~/Documents/mycode/ex8$ ./ex8
You only have one argument. You suck.
anno@anno-m:~/Documents/mycode/ex8$ ./ex8 one
Here's your arguments:
./ex8
one
anno@anno-m:~/Documents/mycode/ex8$ ./ex8 one two
Here's your arguments:
./ex8
one
two
anno@anno-m:~/Documents/mycode/ex8$ ./ex8 one two three
You have too many arguments. You suck.
-
argc
记录输入的参数个数; -
./ex8
也是参数,并且是argv[0]
,是第一个参数; - 想要从
one
(即argv[1]
)开始输出,把for
语句里面的i=0
改成i=1
就可以了;
参考资料
- C C language Expressions
C C language Expressions
- C C language Expressions
https://en.cppreference.com/w/c/language/expressions
C C language Expressions
用网页提供在线编辑器、编译器 run一下想测试的代码
- 在线编译器 coliru
https://coliru.stacked-crooked.com/
在线编译器 coliru
1、默认的命令是:
g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
2、可以加上自己的参数,写成:
clang -Wall -g main.cpp && ./a.out one two
3、点击 Compile,link and run
网友评论