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 thatstr2
is pointing to, which is now "C programming" due to the previousstrcpy
operation.
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 is the output of the following C code?char *ptr;char mystring[] = "abcdefg";ptr = myString;ptr += 5;
Find output of the below program:int main(){ char str[]={'e','a','r','t','h'}; printf("%s", str); return 0;}
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
What is the output of the following code?#include <stdio.h>int main() { int arr[2][2] = {{1, 2}, {3, 4}}; printf("%d\n", arr[0][2]); 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.