C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 9)
9.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
for(i=0; i<=4; i++)
printf("%s,", names[i]);
return 0;
}
Answer: Option
Explanation:
Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is declared as an pointer to a array of strings.
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names.
Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".
Discussion:
14 comments Page 2 of 2.
Srinivas said:
1 decade ago
Why it is Ritu, Baiju-->3, 4th positions?
We just changed the pointing positions not the values.
It should be Baji Baji.
We just changed the pointing positions not the values.
It should be Baji Baji.
Renju said:
1 decade ago
char *t;
t = names[3];
names[3] = names[4]
names[4] = names[3]
what I did not understand here is, t is pointing to the address of names[3].
So if we change the content in the address of names[3], t should then point to this new value right.
So at the end.. shouldn't names[3] and names[4] be same (which is "Baiju" in this case)?
Can anyone please clear this doubt?
t = names[3];
names[3] = names[4]
names[4] = names[3]
what I did not understand here is, t is pointing to the address of names[3].
So if we change the content in the address of names[3], t should then point to this new value right.
So at the end.. shouldn't names[3] and names[4] be same (which is "Baiju" in this case)?
Can anyone please clear this doubt?
Natraj said:
1 decade ago
I<=4 MEANS how fourth name alone exchanged to fifth name.
Pratik said:
1 decade ago
aftr swapping the element how does it provide this output
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers