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.
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.
Sreepal V said:
1 decade ago
A command line argument is the
information that follows the program's name on the command line of the operating system. For
example, when you compile a program, you might type something like the following after the
command prompt,
program_name string1 string2 string3 ... (default syntax)
OR
"program_name" "string1" "string2" "string3" ...(correct syntax)
information that follows the program's name on the command line of the operating system. For
example, when you compile a program, you might type something like the following after the
command prompt,
program_name string1 string2 string3 ... (default syntax)
OR
"program_name" "string1" "string2" "string3" ...(correct syntax)
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 ?
Anish said:
1 decade ago
argc=2
argv[0]=sample
argv[1]=*.c
So it prints *.c
argv[0]=sample
argv[1]=*.c
So it prints *.c
Kavithha said:
1 decade ago
Since argv[] is a string, it print ly the string pointed by the argv[1] i.e. *.c .
Stillalone4u said:
1 decade ago
@shamesad
only arg[] prints string !!
So "" not printed it prints only *.c
only arg[] prints string !!
So "" not printed it prints only *.c
AppiReddy said:
1 decade ago
Any one can explain the output of the above program please.
Varun Gupta said:
1 decade ago
@Chandrasekar not a relevant explanation
Chandrasekar said:
1 decade ago
Because printf starts from argv[1]. So it starts from args passed into program.
"Sample" is the program name which is stored in argv[0]
"Sample" is the program name which is stored in argv[0]
Shamshad said:
1 decade ago
Why don't the output be "*.c" ?.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers