Discussion :: Command Line Arguments - Find Output of Program (Q.No.5)
|Sreepal V said: (Dec 18, 2010)|
|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.
|Moshii said: (May 27, 2011)|
|Thanks sreepal for your great ans.|
|Sravan said: (Aug 8, 2011)|
|It is a nice explanation.|
|Arvind Kumar said: (Sep 28, 2011)|
|Please explain it again to make more clear it. !|
|Aseem said: (Nov 2, 2011)|
|Thanks for this "cool" explanation :-)|
|Divya said: (Mar 9, 2012)|
|Thanks sreepal:) very nice explanation|
|Anurag Kumar said: (Aug 17, 2012)|
|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|
|Jhunu said: (Aug 18, 2012)|
|Merlin said: (Aug 27, 2012)|
|Thanks sreepal, your explanation is easy to understand.|
|Anil Kumar said: (Aug 31, 2012)|
|Rishi said: (Mar 4, 2013)|
|Why only argc as 1? I can always take 2 or 3, then the answer will vary right.|
|Praveen said: (Apr 24, 2013)|
|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.|
|Deepak said: (Sep 30, 2013)|
|But it is printing 4 since in the 4th argument loop will be terminated. Then how 4 is printed?|
|Shalini said: (Nov 5, 2013)|
|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.
|Jasneet said: (Aug 8, 2014)|
|If we replace "++i" with "i++"..then it shows stack overflow.
Can any one explain why ?
|Lokesh C P said: (Sep 15, 2014)|
|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.|
|Learner said: (Mar 3, 2015)|
|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 :').|
|Ashok said: (Dec 31, 2015)|
|But void function can't be to return any values how is this work?|
|Kelvin D said: (Mar 25, 2016)|
|@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.
|Sheetal said: (Apr 14, 2016)|
|It is showing the answer as 2 3 4 in turbo C.|
|Pratik said: (Jul 23, 2016)|
|Why argc is 1?|
|Vasu said: (Oct 11, 2016)|
|argc is 1 because default takes one command line argument that is a.out, a.out is one file.|
|Dilip said: (Mar 5, 2017)|
|But how we can call main from other function?|
|Divya said: (Jun 16, 2017)|
|Because main() is also a function where the program will start.|
|Shreyaskar Nath said: (Oct 23, 2017)|
|Answer will be 2, 3, 4. Option C is the correct one.|
|Rohan said: (Jun 10, 2018)|
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..
Step 3: call the function fun with value 1 [i.e. fun(1);]
void fun(int i)
Step 4: -> First Time: if (1!=4)
-> control is transfer to main function with arguement 2
-> prints 2 and so on............
|Sravya said: (Aug 27, 2018)|
|Good explanation @Shalini.|
|Subhashini said: (Mar 29, 2019)|
|Thank you @Rohan.|
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.