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.
Please log in or register to report this question.