C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 13)
13.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample monday tuesday wednesday thursday
cmd> sample monday tuesday wednesday thursday
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
while(--argc>0)
printf("%s", *++argv);
return 0;
}
Discussion:
9 comments Page 1 of 1.
Yurii said:
7 years ago
GCC returns the following result:
Samplemondaytuesdaywednesdaythursday. It is not in the given option.
Samplemondaytuesdaywednesdaythursday. It is not in the given option.
Yash said:
9 years ago
I can't understand it easily. Please explain it in simple way.
Karan kumar said:
9 years ago
It is not running in code block showing anything. So please help me.
Parth said:
10 years ago
After executing this code in code blocks, I get this as output:
"sample monday tuesday wednesday thursday" which doesn't match any of the options.
"sample monday tuesday wednesday thursday" which doesn't match any of the options.
S.s said:
1 decade ago
I can't understand all those answers I am expecting clear and simple explanation.
Priyu said:
1 decade ago
I am not getting the answer because there is no parenthesis[] after argv[] so how could it will accept d argument ?
Amarender Surepally said:
1 decade ago
Initially argc=5, argv=0;
step1: --argc=4>0 ===> *++argv ==> argv[1] ==> monday;
step2: --argc=3>0 ===> *++argv ==> argv[2] ==> tuesday;
step3: --argc=2>0 ===> *++argv ==> argv[3] ==> wednesday;
step4: --argc=1>0 ===> *++argv ==> argv[4] ==> thursday;
step5: --argc=0>0 FALSE.
step1: --argc=4>0 ===> *++argv ==> argv[1] ==> monday;
step2: --argc=3>0 ===> *++argv ==> argv[2] ==> tuesday;
step3: --argc=2>0 ===> *++argv ==> argv[3] ==> wednesday;
step4: --argc=1>0 ===> *++argv ==> argv[4] ==> thursday;
step5: --argc=0>0 FALSE.
Sumali said:
1 decade ago
According to character verifying whether the required string of first character is greater than zero. If it is strings of all characters is printed hence the given condition is satisfied.
Prateek said:
1 decade ago
I THINK THE GIVEN ANSWER IS RIGHT BECAUSE
arg[v] means arg[0] so ++arg[v] points to arg[v]
and then according to while loop all the strings should
be printed to the limit when argc is greater than zero
arg[v] means arg[0] so ++arg[v] points to arg[v]
and then according to while loop all the strings should
be printed to the limit when argc is greater than zero
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers