is there any method to print a character or string without using ; ?

Dani AI

Generated

A common puzzle. asked whether a character/string can be printed without using a semicolon; gave the expected snark; already demonstrated the usual trick of calling a printing function inside a control-expression so there is no trailing semicolon. The same idea works with other output functions and other control statements, and it is legal C — but it is a style/golf trick, not good practice for production code.

Example alternatives that avoid any ; in the source:

#include <stdio.h>

int main(void)
{
    switch(puts("Hello, world")){}
}
#include <stdio.h>

int main(void)
{
    if(putchar('A')){}
}
#include <unistd.h>

int main(void)
{
    switch(write(1, "Hi\n", 3)){}
}

Notes and cautions: these examples rely on the side effect and return value of the output call; headers such as <stdio.h> and <unistd.h> should be present to avoid warnings. For single characters prefer putchar and for simple newline-terminated strings puts (putchar reference, puts reference). For low-level, POSIX output use write (man page). Such tricks are acceptable for puzzles or code-golf but reduce clarity and maintainability.

Recommended Answers

All 2 Replies

Sure, why not ask more questions without effort.

You can do something like this:

if(printf("Hello"))
{
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.