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.

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!

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.

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.

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)

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?

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.

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.

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.

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.

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..


Post your comments here:

Your comments will be displayed after verification.