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 2 of 2.
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.....
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.....
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!
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!
Vidhya Balan said:
1 decade ago
Tell me - what is command line argument?
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
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
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"
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"
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.
On printing argc,argv,its o/p will be 3 Good.
Because count value is 3 here.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers