Discussion
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample 1 2 3
cmd> sample 2 2 3
cmd> sample 3 2 3
/* sample.c */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%s\n", argv[0]);
return 0;
}
Answer: C
Correct answer: sample
argv[0] holds the name by which the program was invoked, and the command line here begins with sample, so that is what is printed - whatever arguments follow.
The three invocations differ only in their arguments, and argv[0] is unaffected by them. Note that the exact contents are a platform convention: Unix-like systems generally give the string as typed, while Turbo C under DOS supplies the full uppercase path, which is what the companion question reflects.
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