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 2 of 3.
Aloke said:
1 decade ago
@anish
But argv[1]="*.c"
Where is the " " gone ?
Why they are missing please explain me ?
But argv[1]="*.c"
Where is the " " gone ?
Why they are missing please explain me ?
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!!
Raj said:
1 decade ago
Am getting the output as the list of files and folders in the current directory.Why is it so? Some one explain please.
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
KomPri said:
1 decade ago
Thanks Stillalone4U.
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.
Gcp said:
1 decade ago
Can anybody tell me what's with the int argv[]. How can characters be the elements of integer strings.
Shail said:
1 decade ago
Warning: format \'%s\' expects type \'char *\', but argument 2 has type \'int\'.
Mangusta said:
1 decade ago
Hey, But argument is char * argv, not char* *argv or char* argv[].
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).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers