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.
Raju Singh Kumai said:
1 decade ago
i=strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");
The statement will be solved in this order :
1-> strcpy(str2,str1))
This will cause the value of str1 "dills" to be copied in str2.
2->strcat(str3,strcpy(str2,str1)))
The value of strcpy(str2,str1) will be -> "dills"
So the result of iot will be "daffodills"
3->strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");
Or in short[strcmp("daffodills","daffodills")]
In strcmp if both are same then 0 is return otherwise -1 or 1 is return depending on Left or right Exp is Greater.
So 0 will be return.
The statement will be solved in this order :
1-> strcpy(str2,str1))
This will cause the value of str1 "dills" to be copied in str2.
2->strcat(str3,strcpy(str2,str1)))
The value of strcpy(str2,str1) will be -> "dills"
So the result of iot will be "daffodills"
3->strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");
Or in short[strcmp("daffodills","daffodills")]
In strcmp if both are same then 0 is return otherwise -1 or 1 is return depending on Left or right Exp is Greater.
So 0 will be return.
Konstantinos said:
1 decade ago
static char str3[] = "Daffo";
//6 chars length (5+1) memory reserved!
strcat (str3, strcpy (str2, str1));
//copy in to a 6 chars reserved memory 11 chars (10+1) !
As you can see from the below.
char *strcat (char *dest, const char *src).
Dest -- This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.
From what you see the above should produce a memory corruption!
//6 chars length (5+1) memory reserved!
strcat (str3, strcpy (str2, str1));
//copy in to a 6 chars reserved memory 11 chars (10+1) !
As you can see from the below.
char *strcat (char *dest, const char *src).
Dest -- This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.
From what you see the above should produce a memory corruption!
Mallesh said:
7 months ago
Here str1 [] = "dills".
str2[20]
str3[] = "Daffo"
int i
The condition is checked by the form precedence and associativity
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills")
printf("%d\n", i)
i = strcmp(strcat(str3, "dills"), "Daffodills")
i = strcmp("Daffodills", "Daffodills")
i = 0
Then, by the stcmp conditions,
if Str 1 == str2 -> 0.
str1 > str2 -> +ve value.
str1 < str2 -> -ve value.
str2[20]
str3[] = "Daffo"
int i
The condition is checked by the form precedence and associativity
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills")
printf("%d\n", i)
i = strcmp(strcat(str3, "dills"), "Daffodills")
i = strcmp("Daffodills", "Daffodills")
i = 0
Then, by the stcmp conditions,
if Str 1 == str2 -> 0.
str1 > str2 -> +ve value.
str1 < str2 -> -ve value.
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.
Mounika.p said:
1 decade ago
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
first it will copy str1 to str2.and then by string concatinating str3 with str2..it will give Daffodills.after that string comparision between to strings.these two strings are same.so strcmp of this two strings gives 0 value...that's enough about this programme...
first it will copy str1 to str2.and then by string concatinating str3 with str2..it will give Daffodills.after that string comparision between to strings.these two strings are same.so strcmp of this two strings gives 0 value...that's enough about this programme...
Mano said:
1 decade ago
i=strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");
//str1=dills
//str2=char[20]
//str3=daffo
1. strrcpy(str2,str1):
str2=dills //str1 value is copied to str2
2. strcat(str3,str2):
daffodills//str3+str2
3. strcmp(daffodills,daffodills)
They are equal hence 0 is correct.
//str1=dills
//str2=char[20]
//str3=daffo
1. strrcpy(str2,str1):
str2=dills //str1 value is copied to str2
2. strcat(str3,str2):
daffodills//str3+str2
3. strcmp(daffodills,daffodills)
They are equal hence 0 is correct.
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)
Shabana said:
1 decade ago
Condition is that string cmp.
1- Strcpy. the str1 is copy to str2 it will print dills.
2- The str2 and str3 multiply done and it will DOffodills it will matches the Daffo. Both ASCII values are same so it will 0.
1- Strcpy. the str1 is copy to str2 it will print dills.
2- The str2 and str3 multiply done and it will DOffodills it will matches the Daffo. Both ASCII values are same so it will 0.
Harish said:
8 years ago
Str1=dills
Strcpy(str2,str1) it gives str2 = dills
Strcat(str3,str2) gives str3 = daffodills
Strcmp(str3,"daffodils")=it gives 0 ,in string comparison if condition is true it will return 0 else 1.
Strcpy(str2,str1) it gives str2 = dills
Strcat(str3,str2) gives str3 = daffodills
Strcmp(str3,"daffodils")=it gives 0 ,in string comparison if condition is true it will return 0 else 1.
Mohan said:
7 years ago
Str1=dills.
Strcpy(str2,str1) it gives str2 = dills.
Strcat(str3,str2) gives str3 = daffodills
Strcmp(str3,"daffodils")=it gives 0 ,in string comparison if condition is true it will return 0 else 1.
Strcpy(str2,str1) it gives str2 = dills.
Strcat(str3,str2) gives str3 = daffodills
Strcmp(str3,"daffodils")=it gives 0 ,in string comparison if condition is true it will return 0 else 1.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers