C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 5)
5.
What will be the output of the program
#include<stdio.h>
void fun(int);
int main(int argc)
{
printf("%d ", argc);
fun(argc);
return 0;
}
void fun(int i)
{
if(i!=4)
main(++i);
}
Discussion:
28 comments Page 2 of 3.
Kelvin D said:
9 years ago
@Learner, when we execute the program after compiling we give a.out, so the value of argc==1.
That's why the value of argc is 1.
That's why the value of argc is 1.
Ashok said:
10 years ago
But void function can't be to return any values how is this work?
Learner said:
1 decade ago
How are we taking the initial value of argc as 1? Since we don't know what arguments have been passed through cmd. Thanks in advance :').
Lokesh C P said:
1 decade ago
Its a recursive function, i++ means increment after the execution of the current statement, in this program value 1 is passed as argument value to main program again, program part below the recursive function call pushed to stack and because of I is post incremented main function calls infinite times, when stack is full it shows the stack overflow.
Sreepal V said:
1 decade ago
A command line argument is the information that follows the program's name on the command line of the operating system. For example, when you compile a program, you might type something like the following after the command prompt
c:>program_name string1 string2 string3 ...
Two special built-in arguments, argc and argv, are used to receive command line arguments. The argc parameter holds the number of arguments on the command line and is an integer. It is always at least 1 because the name of the program qualifies as the first argument. The argv parameter is a pointer to an array of character pointers. Each element in this array points to a command line argument.
So, the given program of initial state of argc is 1(one), therefore, it's will print the output 1 2 3 4. After 3, it's will met the false condition.
c:>program_name string1 string2 string3 ...
Two special built-in arguments, argc and argv, are used to receive command line arguments. The argc parameter holds the number of arguments on the command line and is an integer. It is always at least 1 because the name of the program qualifies as the first argument. The argv parameter is a pointer to an array of character pointers. Each element in this array points to a command line argument.
So, the given program of initial state of argc is 1(one), therefore, it's will print the output 1 2 3 4. After 3, it's will met the false condition.
Shalini said:
1 decade ago
After printing 1 2 3 main again calls function.
It checks if(3!=4) //condition true.
So it calls main(++i) i.e main(4).
So 1 2 3 4 prints next time condition falls so program ends.
It checks if(3!=4) //condition true.
So it calls main(++i) i.e main(4).
So 1 2 3 4 prints next time condition falls so program ends.
Deepak said:
1 decade ago
But it is printing 4 since in the 4th argument loop will be terminated. Then how 4 is printed?
Praveen said:
1 decade ago
Here only one argument is given. i.e. name of program. So. It takes only one argument by default. If you supply more arguments through command line output varies.
Rishi said:
1 decade ago
Why only argc as 1? I can always take 2 or 3, then the answer will vary right.
Anil kumar said:
1 decade ago
Thanks sreepal.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers