[习题13]for 循环 和 字符串数组 char *argv[

2018-09-18  本文已影响15人  AkuRinbu

使用教材

《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794

ex13.c

#include <stdio.h>

int main(int argc,char *argv[]) 
{
    int i = 0;

    // go through each string in argv
    // why am I skipping argv[0]?
    for(i = 1; i < argc; i++) {
        printf("arg %d: %s \n", i, argv[i]);
    }

    // let's make out own array of strings
    char *states[] = {
        "Califonia",
        "Oregon",
        "Washington",
        "Texas"
    };

    int num_states = 4;

    for(i = 0; i < num_states;i++) {
        printf("state %d: %s\n", i, states[i]);
    }

    return 0;
}

Makefile

CC=clang
CFLAGS=-Wall -g

clean:
    rm -f ex13

run

anno@anno-m:~/Documents/mycode/ex13$ make ex13
clang -Wall -g    ex13.c   -o ex13
anno@anno-m:~/Documents/mycode/ex13$ ./ex13
state 0: Califonia
state 1: Oregon
state 2: Washington
state 3: Texas
anno@anno-m:~/Documents/mycode/ex13$ ./ex13 hello world !
arg 1: hello 
arg 2: world 
arg 3: ! 
state 0: Califonia
state 1: Oregon
state 2: Washington
state 3: Texas

Note

What is the difference between NULL, '\0' and 0
https://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0

上一篇 下一篇

猜你喜欢

热点阅读