What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
Question
What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
Solution
The output of the code will be "C programming".
Here's the step by step explanation:
-
The code first declares a character array
str1[]and initializes it with the string "C programming". -
Then it declares another character array
str2[20]without initializing it. -
The
strcpy(str2, str1);function is then called. This function copies the string pointed to bystr1(source) tostr2(destination). -
Finally,
printf("%s\n", str2);prints the string thatstr2is pointing to, which is now "C programming" due to the previousstrcpyoperation.
So, "C programming" is printed to the standard output.
Similar Questions
1.What is the output of the following code?char str[] = "hello";printf("%c\n", str[1]);
What will be the output of the following code?#include <stdio.h>int main() { char str[] = "Programming"; printf("%s\n", str + 3); return 0;}
What will be the output of the following C code? #include <stdio.h> #include <string.h> int main() { char *str = "hello, world"; char str1[9]; strncpy(str1, str, 9); printf("%s %d", str1, strlen(str1)); }
What will be the output of the following C code?const char str1[]="ABCDEF1234567";const char str2[] = "269";len = strcspn(str1, str2);printf("First matching character is at %d\n", len + 1);
What is the output of the following code?char str1[] = "abc";char str2[] = "def";int result = strcmp(str1, str2);if (result < 0) printf("str1 is less than str2\n");else if (result > 0) printf("str1 is greater than str2\n");else printf("str1 is equal to str2\n");
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.