C Programming - Command Line Arguments - Discussion


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

/* sample.c */

int main(int argc, char *argv[])
    printf("%d %s", argc, argv[1]);
    return 0;

[A]. 3 Good
[B]. 2 Good
[C]. Good Morning
[D]. 3 Morning

Answer: Option A


No answer description available for this question.

Aishwarya said: (Dec 3, 2010)  
The 'argc' contains count value and argv[1] contains "Good".

On printing argc,argv,its o/p will be 3 Good.

Because count value is 3 here.

Xyz said: (Dec 27, 2010)  
But sample is file name right arguments are given after file name is specified then good =0,morning =1 .. I don't understand if sample is argument here.

Sundar said: (Mar 22, 2011)  
The given answer is correct. I have tested it.

argc - will contain 3 (count of arguments argv[])

argv[1] = Good
argv[2] = Morning

Therefore, answer is "3 Good"

Prasastha said: (Jun 29, 2011)  
argc=argument count= sample,good,morning=3
argv[0]=by default it stores base address= it stores file name=sample

%d argc=3
%s argv[1]=good

Vidhya Balan said: (Jan 6, 2012)  
Tell me - what is command line argument?

Sundar said: (Jan 6, 2012)  
@Vidhya Balan

Assume your current working directory in DOS is "C:\TURBOC" and your C program name is SAMPLE.C (which is in c:\turboc directory).

If you compile the program successfully, an EXE file SAMPLE.EXE will be created in your current directory i.e "c:\turboc".

You can run it by simply typing its name as given below.

C:\TURBOC>sample.exe Good Morning <press-enter-key>


C:\TURBOC>SAMPLE Good Morning <press-enter-key>

Here "sample" is the 'exe-file-name-or-command' and "Good Morning" is the arguments supplied to the exe-file-or-command called "sample" which is created by compiling the C program "sample.c".

Hope this will help you. Have a nice day!

Jayant Jadhav said: (Feb 20, 2012)  
argc which count number of word or characters separated by space and it holds int value. In this example three words are their including file name.Hence argc print 3.

argv[] this is argument vector array which holes all arguments in string format and store from starting index with 0.

Vijay said: (Mar 30, 2012)  
Can any one explain how actually memory allocation is done in command line argument.

Jogamohan Medak said: (Oct 20, 2012)  
argv[0]=Base address=Sample

%d argc=3 because (argv[0],argv[1],argv[2] only three arguments argv[])

%s argv[1]=Good

Abir Sen said: (Oct 25, 2013)  
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.

Padmapriya said: (Sep 14, 2014)  
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.

Naga Vikas said: (Mar 24, 2015)  
Does this depends on the compilers? Because I get it as 4 Sample.


Anyone please tell me?

Zara said: (Feb 15, 2016)  
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.

Prash... said: (Dec 8, 2016)  
Thanks @Sundar.

Garima said: (May 8, 2017)  
Why it count? Please explain me.

Datts said: (Nov 11, 2018)  
After a first value insertion count is automatically incremented i.e. count++.

Meera said: (Aug 2, 2020)  
I am not understanding this, Please anyone help me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.