C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 19)
19.
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
cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%c", *++argv[2] );
return 0;
}
Discussion:
25 comments Page 1 of 3.
Sushma said:
1 decade ago
argv[0]=sample
argv[1]=friday
argv[2]=tuesday
Here we have printf("%c", *++argv[2] );
argv[2]=tuesday
So *++argv[2] points 2nd character in 'tuesday' that it 'u'.
argv[1]=friday
argv[2]=tuesday
Here we have printf("%c", *++argv[2] );
argv[2]=tuesday
So *++argv[2] points 2nd character in 'tuesday' that it 'u'.
Sundar said:
1 decade ago
@Sushma
Exactly. Thanks for your explanation.
Exactly. Thanks for your explanation.
Yugandhar said:
1 decade ago
Your explanation is gud, thank you.
KIRAN said:
1 decade ago
Thanks sushma.
Dharmvir Kumar said:
1 decade ago
Thanks Sushma Jee.
Yuva said:
1 decade ago
Thanks sushma sister
Sumit said:
1 decade ago
in *++argv[2]
argv[0]=t
argv[1]=u
argv[2]=e
Then whats is the answer.
argv[0]=t
argv[1]=u
argv[2]=e
Then whats is the answer.
Alok said:
1 decade ago
Can anybody explain ? How it's(*++argv[2]) pointing to 'U' ?
Shrikant said:
1 decade ago
argv[0]=sample
argv[1]=friday
argv[2]=tuesday
Here we have printf("%c", *++argv[2] );
argv[2] pointed to the "tuesday" and argv is contains the address of first location and ++ operator is adding two byte(because of int) in argv then it takes the address of next location(u) and * operator is use for printing the value of that location;
argv[1]=friday
argv[2]=tuesday
Here we have printf("%c", *++argv[2] );
argv[2] pointed to the "tuesday" and argv is contains the address of first location and ++ operator is adding two byte(because of int) in argv then it takes the address of next location(u) and * operator is use for printing the value of that location;
Preethi Ginimav said:
1 decade ago
@Sushma Thanks for explanation
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers