Discussion
What will be the output of this program (prg_1.c)?
#include <stdio.h>
int main(int argc,char* argv[])
{
printf("%d",argc);
return 0;
}The program is run as:
C:\TC\BIN>prg_1.exe 1 2 3 4
Answer: B
Correct answer: 5
The runtime hands main two things: argc, the number of command line arguments, and argv, an array of pointers to those arguments as strings. The crucial point is that the program’s own name counts. argv[0] holds the name the program was invoked by, and the arguments the user typed follow from argv[1] onwards. Here the command line is prg_1.exe 1 2 3 4: one program name plus four arguments, so \[ \text{argc} = 1 + 4 = 5. \] It is also worth knowing that argv[argc] is guaranteed to be a null pointer, so the array always has argc + 1 elements - which is what lets a loop walk argv without consulting argc at all. The command line had been lost from this record and is restored above; without it the question has no determinate answer.
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