Open In App

Write a C program to print “Geeks for Geeks” without using a semicolon

First of all we have to understand how printf() function works. Prototype of printf() function is:

int printf( const char *format , ...)

Parameter



printf() returns the total number of characters written to stdout. Therefore it can be used as a condition check in an if condition, while condition, switch case and Macros. Let’s see each of these conditions one by one.

  1. Using if condition: 




#include<stdio.h> 
int main() 
    if (printf("Geeks for Geeks") ) 
    { } 

Time Complexity: O(1)
Auxiliary Space: O(1)



          2. Using while condition: 




#include<stdio.h> 
int main(){ 
    while (!printf( "Geeks for Geeks" )) 
    { } 

Time Complexity: O(n)
Auxiliary Space: O(1)

           3. Using switch case: 




#include<stdio.h> 
int main(){ 
    switch (printf("Geeks for Geeks" )) 
    { } 
}

Time Complexity: O(1)
Auxiliary Space: O(1)

         4. Using Macros: 




#include<stdio.h> 
#define PRINT printf("Geeks for Geeks") 
int main() 
    if (PRINT) 
    { } 

Time Complexity: O(1)
Auxiliary Space: O(1)

Output: Geeks for Geeks

One trivial extension of the above problem: Write a C program to print “;” without using a semicolon 




#include<stdio.h> 
int main() 
// ASCII value of ; is 59 
if (printf("%c", 59)) 

Output: ;

Time Complexity: O(1)

Auxiliary Space: O(1)

This blog is contributed by Shubham Bansal.


Article Tags :