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>
int main(int argc, char **argv)
{
printf("%c\n", **++argv);
return 0;
}
Answer: C
Correct answer: o
Take the expression from the inside out. ++argv advances the pointer so it points at argv[1], and the pre-increment yields the new value.
The first * then gives argv[1] itself, which is the string "one" - a char *. The second * dereferences that, giving the first character, 'o'. The %c conversion prints it. Note that a single * with %s would have printed the whole word, which is what the companion question does.
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