C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 19)
19.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str1[] = "Hello";
char str2[10];
char *t, *s;
s = str1;
t = str2;
while(*t=*s)
*t++ = *s++;
printf("%s\n", str2);
return 0;
}
Discussion:
22 comments Page 1 of 3.
Pawan said:
9 years ago
According to me here,
while(*t=*s)
*t++ = *s++;
Here first *t=*s will take the value, after entering into the while it is again going to overwrite the value and then do the post-increment of the pointer. Now the pointers got next character location and again the same repeats till the while fails because of the array *t comes with the null character.
while(*t=*s)
*t++ = *s++;
Here first *t=*s will take the value, after entering into the while it is again going to overwrite the value and then do the post-increment of the pointer. Now the pointers got next character location and again the same repeats till the while fails because of the array *t comes with the null character.
(2)
Deepak Chauhan said:
10 years ago
*t++ means (*t)++ here the address is not being incremented but the value at the address is being incremented to increment the address it should be written as t++ or *++t;
Ex:
while(*t=*s)
{t++;
s++;}
As per above the progranm will keep incrementing the character h then i, j, k. And will store it in str2 base address.
Ex:
while(*t=*s)
{t++;
s++;}
As per above the progranm will keep incrementing the character h then i, j, k. And will store it in str2 base address.
Kavyashree said:
1 decade ago
Here s will points to the beginig of string str1.
And t will points to the begining address of string str2.
In while loop we are copying the elements from str1 to str2 using pointers s and t untill end of string is reached.
And t will points to the begining address of string str2.
In while loop we are copying the elements from str1 to str2 using pointers s and t untill end of string is reached.
Logu said:
1 decade ago
'\0' equal to zero(0).
i.e '\0' ascii value is 0.
So here, *t=*s is doing the copy the value of *s to *t .
So finally it copy the '\0' to *t. now *t has '\0' i.e zero.
So loop exit. So it prints hello.
i.e '\0' ascii value is 0.
So here, *t=*s is doing the copy the value of *s to *t .
So finally it copy the '\0' to *t. now *t has '\0' i.e zero.
So loop exit. So it prints hello.
Neeraj singla said:
1 decade ago
In while statement the operator is equal to(=) instead of (==) comparing operator so in while condition. It will start copying the elements of *s to *t and condition becomes true and output will be hello.
Napster said:
1 decade ago
Simply look at the condition in the while,it is assignment condition which is always true hence the code will copy the hello into str2. If condition is like as '==' then nothing will be copied into it.
Anish said:
1 decade ago
== is used for comparison ex: a=2; b=2 a==b returns true otherwise false
but = is used for assignment ex: a=1; b=a; then b have 1
in if() any value except 0 or null values is true
but = is used for assignment ex: a=1; b=a; then b have 1
in if() any value except 0 or null values is true
Madhu said:
1 decade ago
But in printf statement str2 is given so it has to be printed which was not assigned any value. How could Hello be printed? Any one explain it please.
Mahalaxmibyahatti said:
1 decade ago
Actually the condition in the while loop is having assignment operator i.e = and not == so str1 will be copied to str2 till end of string is reached.
Harish said:
1 decade ago
There is assignment operator in while whenthe str2 will end it can't assign any value or 0 so the loop will be false and the loop will be exit
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers