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.
Subhashini said:
4 years ago
Thank you @Rohan.
Sravya said:
4 years ago
Good explanation @Shalini.
Rohan said:
5 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............
}
Shreyaskar Nath said:
5 years ago
Answer will be 2, 3, 4. Option C is the correct one.
Divya said:
6 years ago
Because main() is also a function where the program will start.
Dilip said:
6 years ago
But how we can call main from other function?
Vasu said:
6 years ago
argc is 1 because default takes one command line argument that is a.out, a.out is one file.
Pratik said:
7 years ago
Why argc is 1?
Sheetal said:
7 years ago
It is showing the answer as 2 3 4 in turbo C.
Kelvin D said:
7 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers