C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 21)
21.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
    int i;
    for(i=1; i<=3; i++)
        printf("%u\n", &argv[i]);
    return 0;
}

If the first value printed by the above program is 65517, what will be the rest of output?

65525 65531
65519 65521
65517 65517
65521 65525
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 2 of 2.

San said:   10 years ago
Your question is not correct.

You should define the platform property in the question.

Amit said:   10 years ago
Output depend upon pointer size if pointer is two byte than option B is correct. If pointer size 4 byte option D is correct.

AADESH said:   10 years ago
Answer is C because each pointer argv points the each command line argument i.e. argv[0] = "my prog.c", argv[1] = "one" and so on.

Now when we apply arithmetic operator on pointer then it the jump of number of locations accordingly it pointing.

Now the number of locations between "one" and "two" are 4 and between "two" and "three" are also 4. Therefore answer is 65517 + 4 = 65521 and 65521 + 4 = 65525.

Trudnai said:   8 years ago
This is platform dependent, the question is bogous. On 16, 32 and 64 bit environment the results will be different.


Post your comments here:

Your comments will be displayed after verification.