C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 21)
21.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
int i, n;
char *x="Alice";
n = strlen(x);
*x = x[n];
for(i=0; i<=n; i++)
{
printf("%s ", x);
x++;
}
printf("\n", x);
return 0;
}
Answer: Option
Explanation:
If you compile and execute this program in windows platform with Turbo C, it will give "lice ice ce e".
It may give different output in other platforms (depends upon compiler and machine). The online C compiler given in this site will give the Option C as output (it runs on Linux platform).
Discussion:
59 comments Page 5 of 6.
Rohit Jindal said:
1 decade ago
n=strlen(x);gives n=5
*x=x[5]; can be written as *(x+0)=x[5] and x[5] contains
'/0' so 'A' will be replaced by '/0' since
x[0] contains 'A'
x[1] contains 'l'
x[2] contains 'i'
x[3] contains 'c'
x[4] contains 'e'
x[5] contains '/0'
*x=x[5]; can be written as *(x+0)=x[5] and x[5] contains
'/0' so 'A' will be replaced by '/0' since
x[0] contains 'A'
x[1] contains 'l'
x[2] contains 'i'
x[3] contains 'c'
x[4] contains 'e'
x[5] contains '/0'
Amit Wadhe said:
1 decade ago
n=strlen(x);
gives n=5
*x=x[5]; can be written as *(x+0)=x[5]
and
x[5] contains '/0' so 'A' will replcd by '/0' since
Now our string will be like this:
x[0]='\0'
x[1]='l'
x[2]='i'
x[3]='c'
x[4]='e'
x[5]='\0'
So in 1st iteration i print \0lice
2nd : ice
3rd : ce
4th : e
5th : \0
i.e lice ice ce e
gives n=5
*x=x[5]; can be written as *(x+0)=x[5]
and
x[5] contains '/0' so 'A' will replcd by '/0' since
Now our string will be like this:
x[0]='\0'
x[1]='l'
x[2]='i'
x[3]='c'
x[4]='e'
x[5]='\0'
So in 1st iteration i print \0lice
2nd : ice
3rd : ce
4th : e
5th : \0
i.e lice ice ce e
Rupinderjit said:
1 decade ago
Rajesh's explanation is 100% relevant and correct.Thanks buddy.
SonaliA said:
1 decade ago
Not getting following statement.
x[5] contains '/0', so 'A' will replaced by '/0'
How?
x[5] contains '/0', so 'A' will replaced by '/0'
How?
Nandu said:
1 decade ago
@sonalia;'\0' means null value. So it does not print anything.
Ankitjain said:
1 decade ago
Just check in ubuntu if you put comment on line *x = x[n] then only you can got output.
Gouri said:
1 decade ago
I did't get how '\0' is at the first position ?
Ashwini said:
1 decade ago
but x[i]=*(x+i),then how is x[5]=*(x+0) ???
Sakshi said:
1 decade ago
I dont understand that how the pointer after reaching the end gets back to print "lice" and so on...
Vidyu said:
1 decade ago
char *x="Alice"; //means *x points to A
n=strlen(x); //n=5
*x=x[n]; //x[5]='\0' and *x points to A here we are assigning x[5] i.e \0 to to *x by this we are replacing A with \0.
Loop part is easy.
n=strlen(x); //n=5
*x=x[n]; //x[5]='\0' and *x points to A here we are assigning x[5] i.e \0 to to *x by this we are replacing A with \0.
Loop part is easy.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers