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)
{
printf("%s\n", *++argv);
return 0;
}
Answer: B
Correct answer: one
++argv advances the pointer to argv[1] and yields the new value, so *++argv is argv[1] - the first argument after the program name.
With the command line myprog one two three that is the string "one", and %s prints it in full. The pre-increment is what skips the program name: argv[0] holds that, and shifting the pointer past it is a common idiom when a program wants only its arguments.
No comment is present. Be the first to comment.
Loading…
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