NSLog
Boolean flag
iOS development
Objective-C
debugging

How to print Boolean flag in NSLog?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Objective-C, a Boolean value can be logged with NSLog, but the best format depends on whether you want numeric output or readable YES and NO text. For debugging, the most useful pattern is usually a ternary expression that prints a clear string rather than raw 0 or 1.

A BOOL in Objective-C is commonly treated like a small integer, so %d works.

objc
BOOL isEnabled = YES;
NSLog(@"isEnabled = %d", isEnabled);

This prints 1 for YES and 0 for NO. It is technically correct, but it is not always the easiest form to scan in logs.

For most debugging output, a readable string is better than a numeric flag.

objc
BOOL isDebugMode = NO;
NSLog(@"isDebugMode = %@", isDebugMode ? @"YES" : @"NO");

That makes the meaning obvious without requiring you to remember what the numeric value represents.

Log C99 bool Values Too

If you are using the C99 bool type instead of Objective-C BOOL, the same %d approach works because the runtime value is still effectively numeric.

objc
1#include <stdbool.h>
2
3bool isActive = true;
4NSLog(@"isActive = %d", isActive);

If readability matters, you can still use a ternary string expression.

objc
NSLog(@"isActive = %@", isActive ? @"true" : @"false");

Use a Macro for Repeated Flag Logging

If you log Boolean flags often, a small macro keeps the output consistent.

objc
1#define LOG_BOOL(flag) NSLog(@"%s = %@", #flag, (flag) ? @"YES" : @"NO")
2
3BOOL isFeatureEnabled = YES;
4LOG_BOOL(isFeatureEnabled);

This logs both the variable name and its readable value. For quick debugging sessions, that is often more useful than repeatedly writing one-off NSLog statements.

Keep the Log Output Intentional

Logging Booleans looks trivial, but the real question is what kind of debugging signal you want. Numeric output is compact. Text output is clearer. A logging helper macro is best when repeated flags appear in the same subsystem.

The key is consistency. If one part of the code prints 1 and another prints YES, log scanning becomes harder than it needs to be.

Remember That NSLog Is Debug-Oriented

NSLog is fine for development diagnostics, but production logging often needs a more structured approach. If the Boolean represents meaningful state in a shipping system, you may want a real logging framework, subsystem tags, or unified logging instead of scattering raw NSLog calls everywhere.

That does not change how to print the value, but it does change where and why you print it.

One extra benefit of the macro approach is that it keeps the formatting decision in one place. If the team later wants true and false instead of YES and NO, you can change one definition instead of every call site.

Common Pitfalls

  • Using %@ directly with a BOOL instead of converting it to an Objective-C object or string first.
  • Printing only 0 and 1 when the logs would be easier to read as YES and NO.
  • Mixing BOOL and bool without understanding that the logging intent is readability, not just type compatibility.
  • Repeating ad hoc Boolean log formatting throughout the codebase instead of centralizing it.
  • Leaving temporary NSLog flag statements in production paths where structured logging would be more appropriate.

Summary

  • '%d prints a Boolean numerically as 0 or 1.'
  • A ternary expression with %@ is better for readable YES and NO output.
  • The same basic approach works for both BOOL and C99 bool.
  • A small macro can standardize repeated Boolean logging.
  • Prefer clear and consistent log output over the shortest possible format.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.