LearnFrenzy
Sign in Take a free test

Decision Making · Find the Error · Question 7

Bug: mixing && and || without brackets in an access check Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

Access should be granted to admins or editors whose account is active. The admin's account is inactive. What does the program print, and why?

main.c
#include <stdbool.h>
#include <stdio.h>

int main(void) {
    bool is_admin = true, is_editor = false, is_active = false;
    if (is_admin || is_editor && is_active)
        puts("access granted");
    else
        puts("access denied");
    return 0;
}
  • A access denied
  • B access granted – && binds tighter, so the test is is_admin || (is_editor && is_active); brackets are needed: (is_admin || is_editor) && is_active Correct
  • C Nothing
  • D It does not compile
Correct Answer: Option B

Because && has higher precedence than ||, any admin passes regardless of is_active. Compilers warn about && within ||; bracket the intended grouping.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links