Monday, 12 August 2013

Explain the output of this program?

Explain the output of this program?

What is the output of the following program, if we pass to it the
following parameters through the command line:
bcd abcd ab abc
So, since we pass 4 arguments, argc is 4? We initialize i to 2 and then go
and checked argv's from 1 to 3 - my guess would be we add i = 2, and
later, in the next iteration i = 3, and that's 5, so the output would be
5?
void main(int argc, char* argv[]){
char *p, *q;
int i = 2, j = 0, k = 0;
for (;i < argc; i++){
p = argv[i-1]; q = argv[i];
for(j = 0; *q && *p; j++, p++, q++)
if (*p != *q) break;
if (!*p || !*q) k += i;
}
printf("%d",k);
}

No comments:

Post a Comment