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 2 of 3.
Ankita said:
1 decade ago
It simply increase the address by one. But here it can not enter into loop. So answer is "Hello".
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.
Saurabh said:
1 decade ago
@wikilok is current ...
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
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
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.
Mahalaxmi said:
1 decade ago
Actually it is not == it is just = in while loop so it will assign the str1 to str2 till end of string is reached.
Newbie said:
1 decade ago
How it is always a true condition? it will be false condition when *t not equal to *s. rite? here *t and *s are not equal. rite?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers