C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 6)
6.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample "*.c"
cmd> sample "*.c"
/* sample.c */
#include<stdio.h>
int main(int argc, int *argv)
{
int i;
for(i=1; i<argc; i++)
printf("%s\n", argv[i]);
return 0;
}
Discussion:
22 comments Page 1 of 3.
Sundar said:
1 decade ago
@Aloke
I would like to explain it with DOS commands.
C:\Program Files>cd windows media player
C:\Program Files\Windows Media Player>cd..
C:\Program Files>cd "windows media player"
C:\Program Files\Windows Media Player>
The above commands works perfectly in latest versions of DOS (after Windows 98).
But in previous versions of DOS we cannot use commands with white spaces in directly name like below:
c:\Program Files>cd windows media player
If you use like that it will say like "directory not found error". We should enclose the directory name with double quotes like given below:
c:\Program Files>cd "windows media player"
So, The DOS itself ignores the quotes (") and inputs only *.c to the program. This is the reason what you expected.
I hope this will help you! Have a nice day!!
I would like to explain it with DOS commands.
C:\Program Files>cd windows media player
C:\Program Files\Windows Media Player>cd..
C:\Program Files>cd "windows media player"
C:\Program Files\Windows Media Player>
The above commands works perfectly in latest versions of DOS (after Windows 98).
But in previous versions of DOS we cannot use commands with white spaces in directly name like below:
c:\Program Files>cd windows media player
If you use like that it will say like "directory not found error". We should enclose the directory name with double quotes like given below:
c:\Program Files>cd "windows media player"
So, The DOS itself ignores the quotes (") and inputs only *.c to the program. This is the reason what you expected.
I hope this will help you! Have a nice day!!
Tanya said:
7 years ago
Please explain the correct answer.
Prashant Kumar said:
8 years ago
I am getting the output as list of all .c file in the current directory? why it's so?
Mangusta said:
1 decade ago
Disregard my last post, I have mistakenly seen int * argv as char * argv.
Inside "main", argv is treated as pointer and all further arithmetics performed upon it, will depend on actual type of this pointer.
It doesn't matter on 16- and 32-bit systems, if we use:
int * argv instead of char* * argv
Because sizeof(int) = sizeof(char *) on both, therefore argv[i] with (int * argv) will be equal to argv[i] with (char* * argv).
It however does matter on 64-bit and higher systems, because sizeof(int) = 4 but sizeof(char *) >= 8.
Therefore argv[i] with (int * argv) will not be equal to argv[i] with (char* * argv).
Inside "main", argv is treated as pointer and all further arithmetics performed upon it, will depend on actual type of this pointer.
It doesn't matter on 16- and 32-bit systems, if we use:
int * argv instead of char* * argv
Because sizeof(int) = sizeof(char *) on both, therefore argv[i] with (int * argv) will be equal to argv[i] with (char* * argv).
It however does matter on 64-bit and higher systems, because sizeof(int) = 4 but sizeof(char *) >= 8.
Therefore argv[i] with (int * argv) will not be equal to argv[i] with (char* * argv).
Mangusta said:
1 decade ago
Hey, But argument is char * argv, not char* *argv or char* argv[].
Shail said:
1 decade ago
Warning: format \'%s\' expects type \'char *\', but argument 2 has type \'int\'.
Gcp said:
1 decade ago
Can anybody tell me what's with the int argv[]. How can characters be the elements of integer strings.
Yogesh said:
1 decade ago
I'm facing more confusion my gcc compiler print :-sample *.c
But you are given output only *.c
How to print a :- *.c
If any one known please give me correct reason.
But you are given output only *.c
How to print a :- *.c
If any one known please give me correct reason.
KomPri said:
1 decade ago
Thanks Stillalone4U.
Ram said:
1 decade ago
In above program no of arguments are 2..
now condition is for(i=1;i<2;i++) here condition is true
%S needs base adress to print a string we provided argc[1] in printf ..hence the output becomes *.c
now condition is for(i=1;i<2;i++) here condition is true
%S needs base adress to print a string we provided argc[1] in printf ..hence the output becomes *.c
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers