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;
}
Alice
ecilA
Alice lice ice ce e
lice ice ce e
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 4 of 6.

Balaji said:   8 years ago
In gcc compiler, this program is crashed because *x=x[5] (x[5] is not allocated memory ). Am i right?

Dake priyanka said:   6 years ago
In gcc platform shows an segmentation fault, why? I don't understand can you explain anyone of these.
(1)

SonaliA said:   1 decade ago
Not getting following statement.

x[5] contains '/0', so 'A' will replaced by '/0'

How?

Ankitjain said:   1 decade ago
Just check in ubuntu if you put comment on line *x = x[n] then only you can got output.

Gowthami said:   1 decade ago
if x[5] is '/0' then how 'A' will be replaced by '/o'.

Please give clear explanation.

Pratik said:   1 decade ago
I am getting option C in both inbuilt C compiler of indiaBix as well as on Turbo C.

Ankur Sharma said:   1 decade ago
Compiler on this site gives this output:

Alice lice ice ce e

I don't know why ?

Rajesh.T.K. said:   1 decade ago
Can a single variable hold both "Alice" string and base address of an array?

Divyaprabha said:   9 years ago
Guys, please explain What is the last printf("\n", x) doing here?

Priya said:   10 years ago
Am getting option C while compiling. Please help me with right answer.


Post your comments here:

Your comments will be displayed after verification.