C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 4)
4.
What will be the output of the program (sample.c) given below if it is executed from the command line (turbo c under DOS)?
cmd> sample Good Morning
cmd> sample Good Morning
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%d %s", argc, argv[1]);
return 0;
}
Discussion:
17 comments Page 1 of 2.
Meera said:
3 years ago
I am not understanding this, Please anyone help me.
Datts said:
4 years ago
After a first value insertion count is automatically incremented i.e. count++.
Garima said:
6 years ago
Why it count? Please explain me.
Prash... said:
6 years ago
Thanks @Sundar.
Zara said:
7 years ago
Sample Good Morning are three words, so argc will total words like sample = 1 good = 2 and morning = 3 that is total 3 counts and argv contains argv[0] = sample, argv[1] = good, and argv[2] = morning.
So output will be at printf ("%d %s", argc, argv[1]); 3 count at argc, and good at argv[1] that is 3 Good.
So output will be at printf ("%d %s", argc, argv[1]); 3 count at argc, and good at argv[1] that is 3 Good.
Naga Vikas said:
8 years ago
Does this depends on the compilers? Because I get it as 4 Sample.
0--Path.
1--Sample.
2--Good.
3--Morning.
Anyone please tell me?
0--Path.
1--Sample.
2--Good.
3--Morning.
Anyone please tell me?
Padmapriya said:
8 years ago
But I have a doubt guys. Usually the count value will start with 0, but how here started with 1. Is that the rule for argc[]. Please explain.
Abir Sen said:
9 years ago
In linux,
argc = 3 because no of arguments that are passed is 3 in the program.
arv[0] = ./a.out,
argv[1] = Good,
argv[2] = Morning.
argc = 3 because no of arguments that are passed is 3 in the program.
arv[0] = ./a.out,
argv[1] = Good,
argv[2] = Morning.
Jogamohan Medak said:
1 decade ago
argv[0]=Base address=Sample
argv[1]=Good
argv[2]=Morning
%d argc=3 because (argv[0],argv[1],argv[2] only three arguments argv[])
%s argv[1]=Good
argv[1]=Good
argv[2]=Morning
%d argc=3 because (argv[0],argv[1],argv[2] only three arguments argv[])
%s argv[1]=Good
Vijay said:
1 decade ago
Can any one explain how actually memory allocation is done in command line argument.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers