Print “Hello World” Without Using Semicolon

Writing a Hello World program in C without using a semicolon is a classic interview trick and a fun way to understand how C evaluates expressions. In C, every statement ends with a semicolon — but you can embed a function call inside the condition of if, while, or switch statements, which do not require a trailing semicolon after their closing brace.

The trick works because printf() returns the number of characters it printed (a positive integer, which is “true” in C). The original post had no code at all — just a title and “Asked in various interviews.” This rewrite explains the concept properly, shows three methods, and notes when each is appropriate.

The Key Insight

These C constructs do NOT end with a semicolon:

if (condition) { }            /* no semicolon after closing brace */
while (condition) { }         /* no semicolon after closing brace */
switch (expression) { }       /* no semicolon after closing brace */
#include <stdio.h>            /* preprocessor directive — no semicolon */
int main(void) { }            /* function definition — no semicolon */

So if you put printf(...) as the condition, you call it without a semicolon.

Method 1: Using if

/* Hello World without semicolons — using if
 * printf returns the number of characters printed (positive = true)
 * In C99+, main() can exit without explicit return 0.
 * Compile: gcc -std=c99 -Wall -Wextra nosemicolon.c -o nosemicolon */
#include <stdio.h>

int main(void)
{
    if (printf("Hello, World!\n")) {}
}
Output:
Hello, World!

This file contains zero semicolons. Confirmed: grep ';' nosemicolon.c returns nothing.

Method 2: Using while

#include <stdio.h>

int main(void)
{
    while (!printf("Hello, World!\n")) {}
}

The negation ! inverts the return value — printf returns a positive number, so !printf(...) is 0 (false), and the loop body runs once then exits. Without !, the loop would run forever because a non-zero condition keeps looping.

Method 3: Using switch

#include <stdio.h>

int main(void)
{
    switch (printf("Hello, World!\n")) {}
}

switch evaluates its expression — printf runs and prints — then falls through the empty body.

How to Compile

gcc -std=c99 -Wall -Wextra nosemicolon.c -o nosemicolon
./nosemicolon

Use -std=c99 (or newer). In C89/C90, a non-void function that falls off without return is technically undefined behavior. C99 made it valid specifically for main(). Under -ansi (C89), add return 0 — but then you have a semicolon.

Why printf Returns a Non-Zero Value

Call Return value Reason
printf("Hello, World!\n") 14 14 characters printed (13 + newline)
printf("") 0 0 characters — the if body would not execute!
printf("x") 1 1 character

For the if method to work, the string must be non-empty — a zero return from printf would make the condition false and skip printing entirely. In practice, “Hello, World!\n” always prints 14 characters, so this is safe.

The while Method Trick Explained

while (!printf("Hello, World!\n")) {}
         ^                           ^
         prints the string           empty body — loop exits immediately
         returns 14 (truthy)
         !14 = 0 (false)  →  loop condition is false  →  loop runs once

What This Teaches

  • printf returns int — almost every C standard library function returns something. printf returns the number of characters written, or a negative value on error. Most code ignores this return value, but here we use it as a condition.
  • Control structures have no trailing semicolonsif (...) {}, while (...) {}, and switch (...) {} are complete statements by themselves. The block {} closes the statement; no semicolon follows.
  • C99 main() exit behavior — the C99 standard added a special rule: if main() returns without an explicit return statement, it is equivalent to return 0;. This is only for main(), not other functions.
  • Expression statements vs. control flow — function calls as standalone statements always need a semicolon: printf("hello\n");. Inside a condition, they are expressions, not statements — no semicolon needed.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

5 comments on “Print “Hello World” Without Using Semicolon

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>