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 1 of 3.
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.
Rohan said:
7 years ago
@All.
#include<stdio.h>
void fun(int);
Step 1: a function named as fun, takes an integer as argument and return void data type.
int main(int argc)
{
printf("%d ", argc);
Step 2: It will print by default 1 because argc always take atleast 1 argument in the command line arguments..
fun(argc);
Step 3: call the function fun with value 1 [i.e. fun(1);]
return 0;
}
void fun(int i)
{
if(i!=4)
main(++i);
Step 4: -> First Time: if (1!=4)
main(++1) [main(2)]
-> control is transfer to main function with arguement 2
-> prints 2 and so on............
}
#include<stdio.h>
void fun(int);
Step 1: a function named as fun, takes an integer as argument and return void data type.
int main(int argc)
{
printf("%d ", argc);
Step 2: It will print by default 1 because argc always take atleast 1 argument in the command line arguments..
fun(argc);
Step 3: call the function fun with value 1 [i.e. fun(1);]
return 0;
}
void fun(int i)
{
if(i!=4)
main(++i);
Step 4: -> First Time: if (1!=4)
main(++1) [main(2)]
-> control is transfer to main function with arguement 2
-> prints 2 and so on............
}
(1)
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.
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.
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.
Anurag Kumar said:
1 decade ago
I am using turbo c++ , i compiled and execute same program but always showing error that "can't call main within the program in fun(int) unction
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 :').
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.
Jasneet said:
1 decade ago
If we replace "++i" with "i++"..then it shows stack overflow.
Can any one explain why ?
Can any one explain why ?
Deepak said:
1 decade ago
But it is printing 4 since in the 4th argument loop will be terminated. Then how 4 is printed?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers