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 1 of 3.

Tanya said:   7 years ago
Please explain the correct answer.

Prashant Kumar said:   8 years ago
I am getting the output as list of all .c file in the current directory? why it's so?

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).

Mangusta said:   1 decade ago
Hey, But argument is char * argv, not char* *argv or char* argv[].

Shail said:   1 decade ago
Warning: format \'%s\' expects type \'char *\', but argument 2 has type \'int\'.

Gcp said:   1 decade ago
Can anybody tell me what's with the int argv[]. How can characters be the elements of integer strings.

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.

KomPri said:   1 decade ago
Thanks Stillalone4U.

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

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.


Post your comments here:

Your comments will be displayed after verification.