C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 33)
33.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char t;
char *p1 = "India", *p2;
p2=p1;
p1 = "BIX";
printf("%s %s\n", p1, p2);
return 0;
}
Answer: Option
Explanation:
Step 1: char *p1 = "India", *p2; The variable p1 and p2 is declared as an pointer to a character value and p1 is assigned with a value "India".
Step 2: p2=p1; The value of p1 is assigned to variable p2. So p2 contains "India".
Step 3: p1 = "BIX"; The p1 is assigned with a string "BIX"
Step 4: printf("%s %s\n", p1, p2); It prints the value of p1 and p2.
Hence the output of the program is "BIX India".
Discussion:
8 comments Page 1 of 1.
Anna said:
1 decade ago
I think the answer given here is not corect, why because logically p2 points to p1. As p1 changes logically ps should change right?
Shawn said:
1 decade ago
Here p1, p2 are two pointers to char type.
The string content of p1 is copied to p2 by statement p2=p1 (equivalent to strcpy () statement) here no address is referenced by p2.
So if p1 changes its string value, it does not reflect in p2 value.
The string content of p1 is copied to p2 by statement p2=p1 (equivalent to strcpy () statement) here no address is referenced by p2.
So if p1 changes its string value, it does not reflect in p2 value.
Debashree said:
1 decade ago
*p1="India" and p1="BIX" are the both sentences indicates same ??
Neeraj said:
1 decade ago
char *p1 = "India", *p2;
Here,p1 and p2 are char pointer .Difference between string and char pointer is that:1)we cannot assign a string to another whereas we can assign a char pointer to another char pointer,2)once a string has been defined,it cannot be assigned to another set of characters but such an operation is perfectly valid for char pointers.
So,in this statement, p2=p1;the value of p1 is being assigned to p2.
And,in this statement, p1 = "BIX";p1 is initialized with another value.
so, printf("%s %s\n", p1, p2);gives BIX India .
Here,p1 and p2 are char pointer .Difference between string and char pointer is that:1)we cannot assign a string to another whereas we can assign a char pointer to another char pointer,2)once a string has been defined,it cannot be assigned to another set of characters but such an operation is perfectly valid for char pointers.
So,in this statement, p2=p1;the value of p1 is being assigned to p2.
And,in this statement, p1 = "BIX";p1 is initialized with another value.
so, printf("%s %s\n", p1, p2);gives BIX India .
Umang said:
1 decade ago
I am confused *P means value and simply P means address. And not getting whats there in program :).
Arnuld said:
1 decade ago
Program is fine except at 2 places where it dos not follow standard C language:
1) int main(void).
2) char* p1 = "India".
As per standard C language, main can have either two arguments or void:
int main(int argc, char* argv[]).
int main(void).
2nd, string literals are always constant characters, else the program behavior is undefined:
const char* = "India".
Therefore, the correct program will be:
#include<stdio.h>
int main(void)
{
char t;
const char *p1 = "India", *p2;
p2=p1;
p1 = "BIX";
printf("%s %s\n", p1, p2);
return 0;
}
Unfortunately, 3rd thing is: I wonder what was the use of introducing variable "t".
1) int main(void).
2) char* p1 = "India".
As per standard C language, main can have either two arguments or void:
int main(int argc, char* argv[]).
int main(void).
2nd, string literals are always constant characters, else the program behavior is undefined:
const char* = "India".
Therefore, the correct program will be:
#include<stdio.h>
int main(void)
{
char t;
const char *p1 = "India", *p2;
p2=p1;
p1 = "BIX";
printf("%s %s\n", p1, p2);
return 0;
}
Unfortunately, 3rd thing is: I wonder what was the use of introducing variable "t".
Reddy royal said:
5 years ago
Please explain the output.
ANDREA said:
2 years ago
@All.
Anyone, please clear out this.
Since p1 and p2 are character pointers and p1 has been initialized with a literal string (constant) the moment a new value is assigned to p1 (p1="BIX") this value is allocated in a new area of memory. Hence p1 changes as a pointer toward a new memory location which at this point no longer coincides with p2 address (wich are the old address of p1).
Correct me if I am wrong.
Anyone, please clear out this.
Since p1 and p2 are character pointers and p1 has been initialized with a literal string (constant) the moment a new value is assigned to p1 (p1="BIX") this value is allocated in a new area of memory. Hence p1 changes as a pointer toward a new memory location which at this point no longer coincides with p2 address (wich are the old address of p1).
Correct me if I am wrong.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers