C Programming - Command Line Arguments - Discussion

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

int main(int argc, char *argv[])
{
    int i;
    for(i=1; i<argc; i++)
        printf("%c", argv[i][0]);
    return 0;
}
oot
ott
nwh
eoe
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
8 comments Page 1 of 1.

Jogamohan Medak said:   1 decade ago
argv[0]=Base address=myprog
argv[1]=One
argv[2]=Two
argv[3]=Three

Given i=1;i<4; i++;
So,argv[1][0]=O(from One )
argv[2][0]=T(from Two )
argv[3][0]=T(from Three )

So Result=ott;
(1)

Nandhakumar said:   1 decade ago
Here i=1, so argv[i][0]=argv[1][0]=o.

Now i incremented i=2, so argv[i][0]=argv[2][0]=t.

Again i incremented i=3, so argv[i][0]=argv[3][0]=t.

So ott.
(1)

Mayank said:   8 years ago
Shouldn't it show error as argv is 1d array and we are trying to access argv[][];?

Malathie said:   8 years ago
Clear explanation Thanks @Jogamohan Medak.

SAGAR said:   8 years ago
Nice solution, Thanks @Nandhakumar.

Azagumozhi said:   9 years ago
Nice explanation @Nandhakumar.

Praveen said:   8 years ago
Thank you @Nandhakumar.

Merlin said:   1 decade ago
Thanks nandhakumar

Post your comments here:

Your comments will be displayed after verification.