

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Memory Management
incomplete
2: C Program Structure
incomplete
3: Interpreted Quiz
incomplete
4: C Is Compiled
incomplete
5: Comments
incomplete
6: Basic Types
incomplete
7: Strings
incomplete
8: Printing Variables
incomplete
9: Compilation: Types
incomplete
10: Variables
incomplete
11: Constants
incomplete
12: Functions
incomplete
13: Void
incomplete
14: Unit Tests
incomplete
15: Math Operators
incomplete
16: If Statements
incomplete
17: Logical Operators
incomplete
18: Ternary
incomplete
19: Type Sizes
incomplete
20: Sizeof
incomplete
21: For Loop
incomplete
22: While Loop
incomplete
23: Do While Loop
incomplete
24: Pragma Once and Header Guards
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Logical operators let you combine multiple conditions in C. There are three main logical operators you'll use all the time:
&& – Logical AND: true if both conditions are true|| – Logical OR: true if either condition is true! – Logical NOT: inverts a boolean valueint age = 25;
bool has_license = true;
if (age >= 18 && has_license) {
printf("Can drive\n");
}
C uses short-circuit evaluation with logical operators. This means:
&&, if the first condition is false, the second isn't even checked (because the whole thing is already false)||, if the first condition is true, the second isn't checked (because the whole thing is already true)if (x != 0 && 10 / x > 2) {
// The division only happens if x != 0
// This prevents a division by zero error
printf("Safe!\n");
}
Logical NOT (!) has higher precedence than AND (&&), which has higher precedence than OR (||). When in doubt, use parentheses to make your intent crystal clear:
// without parentheses – might be confusing
if (!is_raining && is_sunny || is_weekend)
// with parentheses – much clearer
if ((!is_raining && is_sunny) || is_weekend)
The Sneklang package manager needs an access control system for its private package registry. Take a look at exercise.h and implement the function in exercise.c.
The can_access_registry function should return 1 (true) if a user can access the private registry, or 0 (false) if they cannot.
A user can access the private registry if any of these conditions are met:
In C, we use 1 for true and 0 for false when returning boolean-like values from functions that return int.