C Programming - Command Line Arguments - Discussion

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;
}

[A]. *.c
[B]. "*.c"
[C]. sample *.c
[D]. List of all files and folders in the current directory

Answer: Option A

Explanation:

No answer description available for this question.

Sreepal V said: (Dec 18, 2010)  
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)

Anu said: (Dec 30, 2010)  
Can someone explain about the output of the above program ?

Mary said: (Jan 10, 2011)  
Why don't the output be sample?

Shamshad said: (Jan 27, 2011)  
Why don't the output be "*.c" ?.

Chandrasekar said: (Feb 24, 2011)  
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]

Varun Gupta said: (Mar 18, 2011)  
@Chandrasekar not a relevant explanation

Appireddy said: (Jun 2, 2011)  
Any one can explain the output of the above program please.

Stillalone4U said: (Jun 30, 2011)  
@shamesad

only arg[] prints string !!

So "" not printed it prints only *.c

Kavithha said: (Jul 17, 2011)  
Since argv[] is a string, it print ly the string pointed by the argv[1] i.e. *.c .

Anish said: (Aug 5, 2011)  
argc=2
argv[0]=sample
argv[1]=*.c

So it prints *.c

Aloke said: (Aug 31, 2011)  
@anish

But argv[1]="*.c"

Where is the " " gone ?
Why they are missing please explain me ?

Sundar said: (Aug 31, 2011)  
@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!!

Raj said: (Sep 9, 2011)  
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: (May 7, 2012)  
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

Kompri said: (Aug 17, 2012)  
Thanks Stillalone4U.

Yogesh said: (Dec 3, 2012)  
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.

Gcp said: (May 29, 2013)  
Can anybody tell me what's with the int argv[]. How can characters be the elements of integer strings.

Shail said: (Jul 9, 2013)  
Warning: format \'%s\' expects type \'char *\', but argument 2 has type \'int\'.

Mangusta said: (Aug 21, 2013)  
Hey, But argument is char * argv, not char* *argv or char* argv[].

Mangusta said: (Aug 21, 2013)  
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).

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.