ProgrammingVery hardGATE CS
What is printed? (sizet is an unsigned type.)
What is printed? (size_t is an unsigned type.)
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "ab", t[] = "abcd";
if (strlen(s) - strlen(t) < 0) puts("A");
else puts("B");
printf("%d", (int)strlen(s) - (int)strlen(t) < 0);
return 0;
}- A. B then 1
- B. B then 0
- C. A then 1
- D. A then 0
Correct answer: A. B then 1
strlen returns the unsigned size_t, so 2 - 4 wraps to a huge positive value and the < 0 test is always false, printing B. Casting to int first gives -2 < 0, which is 1.
More C Programming questions
- What is printed? c #include <stdio.h> int f(int x) { int r = 0; switch (x) { case 1: r += 1; default: r += 10; case 2: r += 2; break; case…
- What is the exact output? c #include <stdio.h> void p(int n) { if (n <= 0) return; printf("%d", n); p(n - 1); printf("%d", n); p(n - 2); }…
- What is printed? Assume int is 4 bytes and the array is a 2-D array object of automatic storage. c #include <stdio.h> int main(void) { int…
- What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation. c #include <stdio.h> int…
- What is printed? c int a[5] = {1, 2}; printf("%d %d %zu", a[1], a[2], sizeof a / sizeof a[0]);
- What is printed? c printf("%.1f %d", 7 / 2 2.0, (int)(7 / 2.0 2));