Machine-translated by Google. Questions are authored in English.
Top

Discussion

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;
}

 

  • A.1
  • B.2
  • C.4
  • D.0

Answer: D

Correct answer: 0

Work outwards. strcpy(str2, str1) puts "dills" into str2 and returns it; strcat(str3, ...) appends that to "Daffo", making "Daffodills". Comparing that with "Daffodills" gives 0, since the strings are identical. (str3 is static and sized by its initialiser, so this relies on there being room after it - not something to imitate.)

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.