C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 10)
10.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample one two three
cmd> sample one two three
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
int i=0;
i+=strlen(argv[1]);
while(i>0)
{
printf("%c", argv[1][--i]);
}
return 0;
}
Discussion:
18 comments Page 1 of 2.
Aditya hegde said:
6 years ago
sample-argv[0]
one-argv[1]
two-argv[2]
three-argv[3]
and
s-argv[0][0]
a-argv[0][1]
m-argv[0][2]
p-argv[0][3]
similarly
o-argv[1][0]
n-argv[1][1]
e-argv[1][2]
i=0+3=3
argv[1][--3]=argv[1][2]=e
argv[1][--2]=argv[1][1]=n
argv[1][--1]=argv[1][0]=o
Hence answer is eno.
one-argv[1]
two-argv[2]
three-argv[3]
and
s-argv[0][0]
a-argv[0][1]
m-argv[0][2]
p-argv[0][3]
similarly
o-argv[1][0]
n-argv[1][1]
e-argv[1][2]
i=0+3=3
argv[1][--3]=argv[1][2]=e
argv[1][--2]=argv[1][1]=n
argv[1][--1]=argv[1][0]=o
Hence answer is eno.
(1)
Nitin said:
1 decade ago
Can anybody Tell me How argument is passed in a main function ?
What this argv, *argv means.
What this argv, *argv means.
Satish Rowdy said:
6 years ago
argv[0] = sample
argv[1]= one
argv[2]= two
argv[3]= three
Step-1:
i = i + strlen(argv[1]);
// point out the last element of argv[1] = __e
i = 0 + 3 = 3
Step-2:
While(3>0)
{
argv[1][--1] // 3,2,1 loop Rotates
}
So, the output will be eno.
argv[1]= one
argv[2]= two
argv[3]= three
Step-1:
i = i + strlen(argv[1]);
// point out the last element of argv[1] = __e
i = 0 + 3 = 3
Step-2:
While(3>0)
{
argv[1][--1] // 3,2,1 loop Rotates
}
So, the output will be eno.
Manish roy said:
7 years ago
First of all i+ =strlen(argv[i]) means i=i+strlen(argv[i])
Initially i=0.
So,i=0+strlen(argv[0]), where strlen(argv[0])=6
While(6>0)
argv[1][5] ,here in argv[1] there is no 5th character.
While(5>0)
argv[1][4],here in argv[1] there is no 4th character.
.
.
.
argv[1][3],here argv[1] has e as third character.
Proceding upto argv[1][1]
Finally, the answer is eno.
Initially i=0.
So,i=0+strlen(argv[0]), where strlen(argv[0])=6
While(6>0)
argv[1][5] ,here in argv[1] there is no 5th character.
While(5>0)
argv[1][4],here in argv[1] there is no 4th character.
.
.
.
argv[1][3],here argv[1] has e as third character.
Proceding upto argv[1][1]
Finally, the answer is eno.
Mounika said:
8 years ago
cmd: sample one two three are stored as argv[0], argv[1], argv[2], argv[3]
Here, argv[1] points to "one"
strlen(argv[1]) = 3
So i+=strlen(argv[1])
=> i=i+strlen(argv[1]) = 0+3 = 3
Here, argv[1] points to "one"
strlen(argv[1]) = 3
So i+=strlen(argv[1])
=> i=i+strlen(argv[1]) = 0+3 = 3
Dia gupta said:
8 years ago
Can anyone plz exp how i becomes 3?
i+=strlen(argv[1]) -> i BECOMES 3
i+=strlen(argv[1]) -> i BECOMES 3
Revathy ravi said:
8 years ago
(row) 0 1 2 3 4 5 --->column
0 S a m p l e
1 O n e
2 S e c o n d
i+=strlen(argv[1]) -> i BECOMES 3
while(3>0)
prints argv [1][2] ------!i value is now 2 prints e
while(2>0)
prints argv [1][1] -----!i value is now 1 prints n
while(1>0)
prints argv [1][0] -----!i value is now 1 prints o
loop terminates,
eno is the output.
0 S a m p l e
1 O n e
2 S e c o n d
i+=strlen(argv[1]) -> i BECOMES 3
while(3>0)
prints argv [1][2] ------!i value is now 2 prints e
while(2>0)
prints argv [1][1] -----!i value is now 1 prints n
while(1>0)
prints argv [1][0] -----!i value is now 1 prints o
loop terminates,
eno is the output.
Shruti said:
9 years ago
Can anyone explain it in detail?
Deepak kumar said:
1 decade ago
How eno is output of the program?
Maris said:
1 decade ago
i=i+strlen(argv[1]); //i=0+3;
while(i>0) //while(3>0)
argv[1][--i] //argv[1][3] =>e and
i value decremented to 2[--i]
// argv[1][2]=>n
and i=1[--i] //argv[1][1]=>o
o/p:eno
while(i>0) //while(3>0)
argv[1][--i] //argv[1][3] =>e and
i value decremented to 2[--i]
// argv[1][2]=>n
and i=1[--i] //argv[1][1]=>o
o/p:eno
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers