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

  • format: This is a string that contains a text to be written to stdout.
  • Additional arguments: … (Three dots are called ellipses) which indicates the variable number of arguments depending upon the format string.

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:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  2. Using while condition:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  3. Using switch case:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  4. Using Macros:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

Output: Geeks for Geeks

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: ;

This blog is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.