C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 11)
11.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
static char str1[] = "dills";
static char str2[20];
static char str3[] = "Daffo";
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
printf("%d\n", i);
return 0;
}
Discussion:
31 comments Page 1 of 4.
Gautam said:
1 decade ago
It is simple.
strcpy(str2,str1) : here "dills" will be copied to str2.
Hence str2 contains " dills"
strcat(str3,str2) : "dills" will be concatenated with "daffo"
Hence str3 contains " Daffodills ".
At last strcmp compares with the string str3 with "Daffodills" and pass the value "0" since the string matches.
strcpy(str2,str1) : here "dills" will be copied to str2.
Hence str2 contains " dills"
strcat(str3,str2) : "dills" will be concatenated with "daffo"
Hence str3 contains " Daffodills ".
At last strcmp compares with the string str3 with "Daffodills" and pass the value "0" since the string matches.
AASIF JAVED said:
1 decade ago
Don't pass the value.In a comparing two string return the value 0 if string are equal other wise return 1...
Ravi Kumar said:
1 decade ago
step 1: strcpy(str2,str1) cpoy the str1="dills" into str2.
step 2: strcat(str3,str2) append the dills at the end of Daffo.
step 3: strcmp(str3,"Daffodils") returns the no. of charecters of str3 which are not find in Daffodills.
step 4: since both strings are same so results will be 0(zero)
step 2: strcat(str3,str2) append the dills at the end of Daffo.
step 3: strcmp(str3,"Daffodils") returns the no. of charecters of str3 which are not find in Daffodills.
step 4: since both strings are same so results will be 0(zero)
Arjun Prasad said:
1 decade ago
Will str3 array can store dills also?? I think not as its size is just 6 byte.
So I dont agree with answer...!!!
So I dont agree with answer...!!!
Sajal said:
1 decade ago
Explain the reason please.
Rajesh said:
1 decade ago
I agree with goutham.
Jiten said:
1 decade ago
Thanks Gautam.
Ani said:
1 decade ago
Whats the use of static char?
Naresh said:
1 decade ago
All strings declare in static it cannot modifie. So zero is answer.
Neeraj said:
1 decade ago
static is used just to confuse the students, it dosen't make any difference. static is a type of storage classs.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers