Understanding strcat() functionWrite the correct output for the below C code?#include <stdio.h>#include <string.h>void main(void) { char str1[] = "Godavari"; char str2[20]; char str3[] = "River"; int i; i = strcmp(strcat(str3, strcpy(str2, str1)), "RiverGodavari"); printf("%d\n", i);}Output:
Question
Understanding strcat() functionWrite the correct output for the below C code?#include <stdio.h>#include <string.h>void main(void) { char str1[] = "Godavari"; char str2[20]; char str3[] = "River"; int i; i = strcmp(strcat(str3, strcpy(str2, str1)), "RiverGodavari"); printf("%d\n", i);}Output:
Solution
The output of the given C code will be 0.
Here's the step by step explanation:
-
The strcpy() function copies the string pointed by source (which is str1) to the destination (which is str2). So, after this function, str2 becomes "Godavari".
-
The strcat() function appends the string pointed to by str2 to the end of the string pointed to by str3. So, after this function, str3 becomes "RiverGodavari".
-
The strcmp() function compares the two strings lexicographically. If str1 is equal to str2, it returns 0. If str1 is greater than str2, it returns a positive value. If str1 is less than str2, it returns a negative value. In this case, it compares "RiverGodavari" with "RiverGodavari", so it returns 0.
-
The printf() function prints the value returned by strcmp(), which is 0. So, the output of the program is 0.
Similar Questions
What is the first argument of the strcat() function in C programming?
Understanding strcat() functionWrite the correct output for the below C code?#include <stdio.h>#include <string.h>void main(void) { char str1[] = "Godavari"; char str2[20]; char str3[] = "River"; int i; i = strcmp(strcat(str3, strcpy(str2, str1)), "RiverGodavari"); printf("%d\n", i);}Output:
What is the output of the following code ?#include <stdio.h>#include <string.h>int main() { char str1[] = "Hello"; char str2[] = "World"; char str3[20]; strcpy(str3, strrev(strcat(str1, str2))); printf("%s", str3); return 0;}
What is the action of strcat() function?*1 pointcompare two stringsconcatenate two stringsfind the length of the stringcopies one string to another
What is the output of the following code? 12345678#include <stdio.h> #include <string.h> int main() { char str[] = "Exploration of Mars"; char sub[] = "Mars"; printf("%s", strstr(str, sub)); return 0; }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.