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
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%d %s", argc, argv[1]);
    return 0;
}
3 Good
2 Good
Good Morning
3 Morning
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

Sundar said:   1 decade ago
@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>

(or)

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:   1 decade ago
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.
argv[0]=sample
argv[1]=Good.....

Zara said:   10 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.
(2)

Sundar said:   1 decade ago
The given answer is correct. I have tested it.

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

argv[0] = C:\TURBOC\SAMPLE.EXE
argv[1] = Good
argv[2] = Morning

Therefore, answer is "3 Good"

Prasastha said:   1 decade ago
argc=argument count= sample,good,morning=3
argv[0]=by default it stores base address= it stores file name=sample
argv[1]=good
argv[2]=morning

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

Xyz said:   1 decade ago
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.

AISHWARYA said:   1 decade ago
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.

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

Padmapriya said:   1 decade 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:   1 decade 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.


Post your comments here:

Your comments will be displayed after verification.