0

I have problem with argc.

Command line I sent: prog 333 jos nije kraj akademske godine

void main(int argc, char *argv[]){
    char pom[40];
    char *ptr;
    strcpy(pom, argv[5]+3);
    printf("%s;", pom);
    ptr=strchr(argv[6], 'd');
    printf("%s;", ptr);
    printf("%d.", argc);

    return 0;
}

I get j;demske;8.

Why is it 8 and not 7. I should get number of parameters sent.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Unknow123
  • 19
  • 2

2 Answers2

1

"akademske" is argv[6] so you have arguments argv[0] to argv[7]. So these are 8 arguments. argv[0] is the name of your program.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1

Taking into account the program output it seems that these sequence of wards

 prog 333 jos nije kraj akademske godine

is command line arguments. So including the program name itself that corresponds to the argv[0] argc is equal to 8.

From the C Standard (5.1.2.2.1 Program startup)

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name;

argv[5] is the string kraj. So these statements

strcpy(pom, argv[5]+3);
printf("%s;", pom);

output 'j'.

And these statements

ptr=strchr(argv[6], 'd');
printf("%s;", ptr);

output demske.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335