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; }
Question
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; }
Solution
The output of the code will be "Mars".
Here's the step by step explanation:
-
The code includes the standard input/output library
stdio.hand the string librarystring.hin C programming language. -
The
main()function is declared, which is the entry point of the program. -
Inside the
main()function, a character arraystr[]is declared and initialized with the string "Exploration of Mars". -
Another character array
sub[]is declared and initialized with the string "Mars". -
The
printf()function is used to print the output of thestrstr()function. Thestrstr()function is a built-in function in C which returns a pointer to the first occurrence of the substringsubin the stringstr. If the substring is not found, it returns a null pointer. -
In this case, the
strstr()function finds the substring "Mars" in the string "Exploration of Mars" and returns a pointer to the start of this substring. -
The
printf()function then prints the string starting from this pointer, which is "Mars". -
The
main()function returns 0, indicating successful execution of the program.
Similar Questions
What is the output for the following code?
What will be the output of the following code snippet?
What is the output for the following code?123456789#include <stdio.h>int main(){ char ch = '1'; for (ch = '5'; ch <= '8'; ch++) putchar(ch); return (0);}
What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}
What is the output for the following code?12345678910111213#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i % 5 == 0) { i++; continue; } printf("%d ", i); i++; } 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.