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

使用教材

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

ex13.c

#include 

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

  • 操作系统将argc设为argv 数组的个数,即参数个数,第一个参数是程序名,即argv[0]
  • 创建字符串数组char *argv[]char *states[],用%s输出单个元素;

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

你可能感兴趣的:([习题13]for 循环 和 字符串数组 char *argv[])