美文网首页
[习题8]if 、else if 、else、布尔运算符(+在线

[习题8]if 、else if 、else、布尔运算符(+在线

作者: AkuRinbu | 来源:发表于2018-09-12 10:14 被阅读1次

使用教材

《“笨办法” 学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编译器安装,参见

https://www.jianshu.com/p/f7e190e9f4c5

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

https://en.cppreference.com/w/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/
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

在线编译器 coliru

相关文章

网友评论

      本文标题:[习题8]if 、else if 、else、布尔运算符(+在线

      本文链接:https://www.haomeiwen.com/subject/bqvmgftx.html