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.
Moshii said:
1 decade ago
Thanks sreepal for your great ans.
Sravan said:
1 decade ago
It is a nice explanation.
Arvind kumar said:
1 decade ago
Please explain it again to make more clear it. !
Aseem said:
1 decade ago
Thanks for this "cool" explanation :-)
Divya said:
1 decade ago
Thanks sreepal:) very nice explanation
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
Jhunu said:
1 decade ago
Thanks sreepal.
Merlin said:
1 decade ago
Thanks sreepal, your explanation is easy to understand.
Anil kumar said:
1 decade ago
Thanks sreepal.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers