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;
}
0
1
2
4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
31 comments Page 2 of 4.

Satwika said:   9 years ago
Nice explanation @Gautam.

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!

Anusha.v said:   1 decade ago
I agree with @Goutham's answer, but yet I didn't understood what is the role of static char in this programme?

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.

Harsh said:   1 decade ago
As strings are immutable how is the strings modified.

Sus said:   1 decade ago
First it will copy str1 value to str2, then it will concatenate, and then compare.

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.

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.

Kritika said:   1 decade ago
strings are immutable and of reference type, that's y this copy in str3 is allowed.

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...


Post your comments here:

Your comments will be displayed after verification.