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.

Rookie said:   1 decade ago
As said by Shanmugam, argv indicates argv[0].++argv points to argv[1]...now *argv[1] is a pointer to second argument vector.

**argv[1] contains the value "friday".But as we are printing "%c",

we get only the first character. i.e. "f".. thats why the ans.

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.

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.

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.

Shanmugam said:   1 decade ago
argv[0] = "sample";
argv[1] = "friday";
argv[2] = "thuesday";
argv[3] = "sunday";

argv indicates argv[0].so ++argv increments to the next 1-D array.so it points argv[1].Then **argv[1] is points 'f'.

Bhimeswar said:   1 decade ago
Here in main brackets we write chaar *argv[] so the base address goes to*argv[0]if we inrcrement second string address is *argv[1]

**argv[1] means string but in printf we wrote %c only so it displays first character f.

Hariom said:   1 decade ago
Can't understand logic, please help me to undestnd the logic behind it.

I can't understand the roll of dpuble pointer.

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.

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?


Post your comments here:

Your comments will be displayed after verification.