C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 8)
8.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", **++argv);
    return 0;
}
s
f
sample
friday
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

Shivathmika said:   5 years ago
The argv indicates the sample first.
argv[0]=sample
argv[1]=friday
argv[2]=tuesday
argv[3]=saturday..
and ++argv states that aa pre increment so now it points to argv[1] =friday...
And we used the access specifier %C that means we need a char from the above one.

So the answer is f.

Achsah said:   8 years ago
First of all, char* argv[] declaration is same as char **argv. this char **argv is the character array pointer.

In this program, we are incrementing the array, so it goes from argv[0] to argv[1] and we are printing only one char %c so it prints the first letter alone.

J.sangeetha said:   9 years ago
I want a clear explanation for this question. Please provide me.

Rajesh Kumar said:   9 years ago
Friends Shanmugam provides complete solution of this problem. Read pointer carefully, it'll help you.

Saurabh Dhakate said:   9 years ago
But how would it take those arguments as there is no scanf statement inside of the code?

Abp said:   10 years ago
Yes if there was %s in out then output would be friday.

Akash kumar said:   1 decade ago
Since in the printf the %c is written so the output come f if %s is given then at this case it will friday.

Cherry said:   1 decade ago
@Alli.
Meaning of **argv[] is [] takes the first precedence so, it is the array of pointer to pointer and answer for above program is argv[1] is "friday" with in this string(friday) initially pointer shows 'f' now increment it
so,
**argv[1]='r'
Hope you understand.

Alli said:   1 decade ago
Give complete meaning for **argv[]

Deepika said:   1 decade ago
Why **argv[] is used?


Post your comments here:

Your comments will be displayed after verification.