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 6 of 6.
Nadeem said:
1 decade ago
In gcc giving segmentation fault.
Parikshit said:
1 decade ago
But at first iteration it is pointing to null value, so in first iteration it is space, & onword lice ice ce e so how it points to second character at second iteration.
Vinay said:
1 decade ago
Option D is compiler dependent, run this program on turbo c in windows and then then in linux you will understand.
Kittu said:
1 decade ago
How option d can any one explain?.
Rajesh said:
1 decade ago
See dude *x=*x[] one and only same for char type declaration
here string length is five at the begining .
now intially *x=x[5]; so x represents the base address of array x[5] i.e x pointing to first element of array x[5] ie A , so its prints Alice ,next
x++ , so x is pointing to l , then it prints lice ,
at last x comes out of loop pointing to e , so it prints e.
thats it
here string length is five at the begining .
now intially *x=x[5]; so x represents the base address of array x[5] i.e x pointing to first element of array x[5] ie A , so its prints Alice ,next
x++ , so x is pointing to l , then it prints lice ,
at last x comes out of loop pointing to e , so it prints e.
thats it
Vasuroshan said:
1 decade ago
I will explain u guys for linux platform for this the answer is option 'c'.
first see this prog...Actually it is like above prog but with more abstract
#include<string.h>
int main()
{
int i, n;
char *x="Alice";
n = strlen(x);
printf("%d\n",n);
*x = x[n];
for(i=0; i<=n; i++)
{
printf("%s ", x);
printf(" ");
x++;
}
printf("\n", x);
return 0;
}
for this o/p is5
Alice lice ice ce e
so first printf in for loop it prints Alice next space shows next iteration and x++ shows lice and x++ then lice and loop continues and atlast x is e.....
hope u understand ....
sorry for my english...
first see this prog...Actually it is like above prog but with more abstract
#include<string.h>
int main()
{
int i, n;
char *x="Alice";
n = strlen(x);
printf("%d\n",n);
*x = x[n];
for(i=0; i<=n; i++)
{
printf("%s ", x);
printf(" ");
x++;
}
printf("\n", x);
return 0;
}
for this o/p is5
Alice lice ice ce e
so first printf in for loop it prints Alice next space shows next iteration and x++ shows lice and x++ then lice and loop continues and atlast x is e.....
hope u understand ....
sorry for my english...
Aishwarya said:
1 decade ago
Can anyone explain this?
Frank said:
2 decades ago
Please explain.
Aparna said:
2 decades ago
Can you please explain this ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers