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 1 of 2.

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.

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.

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.

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

You should define the platform property in the question.

Sam said:   1 decade ago
Since first value is 65517 and argv is a pointer and pointer always increments by two therefore next two values are 65519 and 65521.

George said:   1 decade ago
All, please note that the offset calculated from the base pointer, is dependent on architecture of the system.

In DOS,a 16 bit OS, DOS uses only 16 bits of the address bus of the CPU(even if there really are 32 lines)- the sizeof(int) would be 2, the sizeof (void *) would also be 2.

On Win32, Linux 32 - 32 Lines are used (if 32 or 64 lines are present) and hence sizeof(int) and sizeof(void*) would be 4.

On Win64, Linux 64 - all 64 lines of the CPU are used(64 lines need to be present in the hardware ), sizeof(int) and sizeof(void *) would be 8.

Please specify the size of a pointer , so that the person doing the test can calculate the actual offset.

Pinky said:   1 decade ago
Yes it should print 2nd 3rd and 4th argument address. Right?

Johnny said:   1 decade ago
Shouldn't it print 3 values. i = 1; 1 <= 3; ++i

first time is 1
second time is 2
third time is 3 which passes the check of <=3

65519
65521
65523

should be the output correct?

Tim said:   1 decade ago
And on a 64-bit machine.... As the others said, this is architecture dependent.

Sudarshan said:   1 decade ago
There is nothing in general fixed size of pointer.it is platform based.basically address are in form of unsingned integer.therefore size depends on size of pointer which is platform dependent.
in 16-bit=2(this will give the option B).
in 32-bit=4(this will give option D).

Overall question is insufficient platform should mentioned over there for correct option.
(1)


Post your comments here:

Your comments will be displayed after verification.