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.
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.
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.
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
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)
Ani said:
1 decade ago
@maris has explained it right..only it shud b decremented first..argv[1][--i])// argv[1][2]..argv[1][1]..argv[1][0]
Please Note: Loop runs for 0 also bcoz at the time of entry check while first check i>0 ie 1>0 and then inside loop it decrements it to 0.
Please Note: Loop runs for 0 also bcoz at the time of entry check while first check i>0 ie 1>0 and then inside loop it decrements it to 0.
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.
Rajat Jain said:
1 decade ago
i=i+strlen(argv[1]); //i=0+3;
while(i>0) //while(3>0)
argv[1][--i] //argv[1][2] =>e and
i value decremented to 2[--i]
// argv[1][2]=>n
and i=1[--i] //argv[1][1]=>o
How is it ??? argv[1]=3
argv[1][2] =>e
while(i>0) //while(3>0)
argv[1][--i] //argv[1][2] =>e and
i value decremented to 2[--i]
// argv[1][2]=>n
and i=1[--i] //argv[1][1]=>o
How is it ??? argv[1]=3
argv[1][2] =>e
Vaibhav said:
1 decade ago
Explanation-.
0th row - sample.
1st row - one.
2nd row- second.
3rd row - third.
Now argv[1][2] means 1st row and 2nd column which is e.
0 1 2 3 4 5
S a m p l e
O n e
S e c o n d
Please correct me if I am wrong. Thanks.
0th row - sample.
1st row - one.
2nd row- second.
3rd row - third.
Now argv[1][2] means 1st row and 2nd column which is e.
0 1 2 3 4 5
S a m p l e
O n e
S e c o n d
Please correct me if I am wrong. Thanks.
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
Vaibhav sharma said:
1 decade ago
i=i+strlen(argv[1]); //i=0+3;
while(i>0) //while(3>0)
argv[1][--i] //argv[1][2] =>e and
i value decremented to 2[--i]
// argv[1][1]=>n
and i=1[--i] //argv[1][0]=>o
o/p:eno
while(i>0) //while(3>0)
argv[1][--i] //argv[1][2] =>e and
i value decremented to 2[--i]
// argv[1][1]=>n
and i=1[--i] //argv[1][0]=>o
o/p:eno
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers