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"
/* 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;
}
*.c
"*.c"
sample *.c
List of all files and folders in the current directory
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 2 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!!

Aloke said:   1 decade ago
@anish

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

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

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]

Shamshad said:   1 decade ago
Why don't the output be "*.c" ?.

Mary said:   1 decade ago
Why don't the output be sample?


Post your comments here:

Your comments will be displayed after verification.