In the following code, what is the output of printf()? 1:#include <stdio.h> 2:#i
ID: 3889131 • Letter: I
Question
In the following code, what is the output of printf()?
1:#include <stdio.h>
2:#include <stdlib.h>
3:#include <string.h>
4:int main(void)
5:{
6: char *str1 = NULL, *str2 = NULL;
7: int slen1 = 5, slen2 = 5;
8: str1 = malloc(sizeof(char) * (slen1 + 1));
9: str2 = malloc(sizeof(char) * (slen2 + 1));
10: strcpy(str2, "Navid");
11: strcpy(str2, "Navid");
12: if (str1 == str2)
13: printf("(a)");
14: if (strcmp(str1, str2) == 0)
15: printf("(b)");
16: if (!strcmp(str1, str2))
17: printf("(c)");
18: return 0;
19:}
Explanation / Answer
Nothing will print. Because in String comaparision function if both string are equal then it will result 0 otherwise it will give result 1.
Here in the above if conditions the first IF condition will not satify as str1 and str2 are not equal. So it comes down to second IF condition , here also both the strings str1 and str2 are not equal so it will give 1 as result , but 1 ==0 is not satisfied. So it will comes down to third IF condition
Here in this actual result will be one . But it is given ! before condition so it is also not printed. So it doesn't give any result.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.