Discussion
If the different command line arguments are supplied at different times would the output of the following program change?
#include <stdio.h>
int main(int argc, char **argv)
{
printf("%d\n", argv[argc]);
return 0;
}
Answer: B
Correct answer: No
argv[argc] is guaranteed by the standard to be a null pointer, marking the end of the vector. That is true whatever arguments are supplied, so the value printed never changes.
Note the further problem: the format is %d while the argument is a pointer, which is itself undefined behaviour and typically prints 0. The question turns on the guarantee at the end of the array - a program can therefore walk argv until it meets the null pointer without consulting argc at all.
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