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.
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)
Anu said:
1 decade ago
Can someone explain about the output of the above program ?
Mary said:
1 decade ago
Why don't the output be sample?
Shamshad said:
1 decade ago
Why don't the output be "*.c" ?.
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]
Varun Gupta said:
1 decade ago
@Chandrasekar not a relevant explanation
AppiReddy said:
1 decade ago
Any one can explain the output of the above program please.
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
Kavithha said:
1 decade ago
Since argv[] is a string, it print ly the string pointed by the argv[1] i.e. *.c .
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers