Discussion
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?
Answer: B
Correct answer: 65519 65521
The loop prints the addresses of successive elements of argv, and consecutive elements of an array are one element apart. Under Turbo C's 16-bit model a char * occupies 2 bytes.
\[ 65517,\ 65517+2 = 65519,\ 65519+2 = 65521 \]
The step is the size of a pointer, not of the strings pointed at, which is what the distractors misjudge. On a 64-bit system the same program would step by 8, so the arithmetic depends entirely on the platform's pointer width.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development