Select the correct answerPredict the output of below given code.#include <stdio.h>int Function(char *S1){ char *S2 = S1; while(*++S1); return (S1-S2);} int main(){ char *Str = "CodeTantra Coding Castle"; printf("%d", Function(Str)); return 0;}Options2524Segmentation Fault26Garbage Value
Question
Select the correct answerPredict the output of below given code.#include <stdio.h>int Function(char *S1){ char S2 = S1; while(++S1); return (S1-S2);} int main(){ char *Str = "CodeTantra Coding Castle"; printf("%d", Function(Str)); return 0;}Options2524Segmentation Fault26Garbage Value
Solution
The correct answer is 25.
Here's the step by step explanation:
-
The main function calls the Function with a string "CodeTantra Coding Castle" which has 25 characters including the space.
-
In the Function, a pointer S2 is initialized to the start of the string S1.
-
The while loop increments the pointer S1 until it points to the null character at the end of the string.
-
After the loop, S1 points to the end of the string and S2 points to the start of the string.
-
The difference between the two pointers (S1-S2) gives the length of the string, which is 25.
So, the output of the program will be 25.
Similar Questions
Select the correct answerWhat will be the output of the following Python code?>>>ct1="codetantra">>>ct1[:3]>>>Optionsdecododetacodetantra
Select the correct answerWhat will be the output of the following Python code snippet if x=1?x<<2Options1248
Select the correct answerWhat will be the output of the following Python code snippet?c = 2for t in range(c): c -= 2 print (c)Options0-20error01234 …
Select the correct answerWhat will be the output of the following Python code snippet?ct = 'code'for i in range(len(ct)): i.upper()print (ct)Optionsc o d eerrornone of the mentioned0 1 2 3
Select the correct answerWhat will be the output of the following Python code snippet?print('Hola Amigos'.istitle())OptionsFalseErrorNoneTrue
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.