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.
Vikash kr kreshari said:
1 decade ago
But that you have given compiler that gives Alice lice ice ce e.
Rupinderjit said:
1 decade ago
Rajesh's explanation is 100% relevant and correct.Thanks buddy.
Nandu said:
1 decade ago
@sonalia;'\0' means null value. So it does not print anything.
Devi said:
10 years ago
In gcc it gives segmentation fault because of 4th line.
Ramdayal said:
1 decade ago
But in Linux gcc compiler it print segmentation fault.
Gouri said:
1 decade ago
I did't get how '\0' is at the first position ?
Bhup said:
4 years ago
Yes, it's a Segmentation fault (core dumped).
(2)
Deepak S said:
1 decade ago
What is the last printf("\n", x) doing here?
Ashwini said:
1 decade ago
but x[i]=*(x+i),then how is x[5]=*(x+0) ???
Vignesh M said:
10 years ago
Why Linux compiler give segment fault?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers