C Programming - Command Line Arguments - Discussion


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 */

int main(int argc, char *argv[])
    printf("%c", **++argv);
    return 0;

[A]. s
[B]. f
[C]. sample
[D]. friday

Answer: Option B


No answer description available for this question.

Hariom said: (Oct 21, 2010)  
Can't understand logic, please help me to undestnd the logic behind it.

I can't understand the roll of dpuble pointer.

Sugimalathi said: (Jan 11, 2011)  
Explain this answer please.

Shanmugam said: (Jan 31, 2011)  
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'.

Manish said: (Mar 13, 2011)  
I still could not get understood yet. Please further explain.

Lakshmi said: (Mar 31, 2011)  
Its not clear.

Rookie said: (Jun 26, 2011)  
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.

Bhimeswar said: (Jul 21, 2011)  
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.

Deepika said: (Feb 23, 2012)  
Why **argv[] is used?

Alli said: (Apr 6, 2012)  
Give complete meaning for **argv[]

Cherry said: (Jul 1, 2012)  
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
Hope you understand.

Akash Kumar said: (Jan 16, 2014)  
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: (Feb 15, 2016)  
Yes if there was %s in out then output would be friday.

Saurabh Dhakate said: (Apr 6, 2016)  
But how would it take those arguments as there is no scanf statement inside of the code?

Rajesh Kumar said: (Jun 11, 2016)  
Friends Shanmugam provides complete solution of this problem. Read pointer carefully, it'll help you.

J.Sangeetha said: (Aug 27, 2016)  
I want a clear explanation for this question. Please provide me.

Achsah said: (Sep 18, 2017)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.