0

It is one of pset2 youtube video instruction about command-line argument. address is following, https://youtu.be/X8PmYwnbLKM

In 6:56, it shows code like this.

int main(int argc, char*argv[]) {

for(int i=0; i<argc; i++)
    for(int j=0, n=strlen(argv[i]); j<n; j++)
    printf("argv[%d][%d] is %c\n",i,j, argv[i][j]);
    return 0;

}

This code works just fine and it makes me more curious. He didn't declare variable for n and he just used like n=strlen(argv[i]); how can it possible?

doesn't it have to be int n=strlen(argv[i])??

Thank you

1

Actually, he did declare n as an int. int j=0, n=strlen(argv[i]) declares both j and n as integers. It is just like the following statements:

int num, num2, num3;
char x, y;
int foo = 1, bar, goo = 2;

Each of these statements declares multiple vars, some of which are initialized. Note that the vars are separated by commas, and the type (int, char) applies to each var in the list.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I totally didn't think that way. I always think int x,y,z; x=0; like this. Thank you so much for answer!
    – jayko03
    Jan 14 '16 at 0:46
1

in C, you are allowed to declare more than one variable of the same type by specifying the type, then listing variables names, and optionally initialize them, in comma-separated form.

so int j = 0, n = strlen(argv[i]) declares a couple of variables, j and n, both of type int and initializes the former with a 0 and the latter with the value returned from the call to strlen(argv[i]).

1
  • I don't know why I didn't see your answer for my question previously. It really helps me. Thank you so much for your comment!
    – jayko03
    Jan 14 '16 at 3:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .