C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 16)
16.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample 1 2 3
cmd> sample 2 2 3
cmd> sample 3 2 3
cmd> sample 1 2 3
cmd> sample 2 2 3
cmd> sample 3 2 3
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%s\n", argv[0]);
return 0;
}
Discussion:
14 comments Page 2 of 2.
Yogesh said:
8 years ago
We provide program name then number of arguments and then the actual arguments.
Here sample 1 2 3, means sample is program name, 1 means the number of arguments,
but we are providing 2 arguments i.e. 2 and 3, isn't it an error?
Here sample 1 2 3, means sample is program name, 1 means the number of arguments,
but we are providing 2 arguments i.e. 2 and 3, isn't it an error?
Mounika said:
8 years ago
@Yogesh.
Here sample 1 2 3 all are command line arguments. It won't considers as program name then number of arguments and then the actual arguments.we just provide program name and arguments in cmd.
In main function, it only count the number of arguments and store it in argc and the arguments are stored in argv[ ] array.
So sample 1 2 3 are stored are argv[0],argv[1], argv[2], argv[3].
Here sample 1 2 3 all are command line arguments. It won't considers as program name then number of arguments and then the actual arguments.we just provide program name and arguments in cmd.
In main function, it only count the number of arguments and store it in argc and the arguments are stored in argv[ ] array.
So sample 1 2 3 are stored are argv[0],argv[1], argv[2], argv[3].
Rahul said:
8 years ago
Is the previous sample being overwritten by the next one?
Sahil said:
8 years ago
It is so easy because it said arg[0], so on first place "sample" lies.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers