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 2 of 2.

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.

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

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

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

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.