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
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?
Discussion:
14 comments Page 2 of 2.
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.
Indal said:
1 decade ago
Please write the answer in easy way. Please help me!.
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!
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!
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers