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.

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)

Shweta said:   2 decades ago
Why it is taking 4 bytes, pointers take 2 bytes... so answer should be 2 bytes?.. plz can any one help..

Sundar said:   2 decades ago
Hi Friends,

If we run the above program in 16 bit platform (DOS), the output will be

65517 (assume it is the first value printed as mentioned in the question)
65519 (65517 + 2 = 65519 )
65521 (65519 + 2 = 65521 )

If we execute the same program in 32 bit platform (Linux), the output will be

3209207768 (assume it is the first value printed)
3209207772 (3209207768 + 4 = 3209207772)
3209207776 (3209207772 + 4 = 3209207776)

Why this difference?

Because in 16 bit platform the number of bytes required to hold an address of a memory location is 2 bytes.

But in 32 bit platform the number of bytes required to hold an address of a memory location is 4 bytes.

Thats why the address values are ranges in terms of 2 bytes with respect to DOS and 4 bytes with respect to Linux.

Note: All the answers given in this site are based on 16-bit platform.

Hope you understand guys. Have a nice day!

Indal said:   1 decade ago
Please write the answer in easy way. Please help me!.

Deepika said:   1 decade ago
In general pointer will occupy 2 bytes, its given that the first value is in 65517, add two for the subsequent bytes.

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

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?

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.