C Program without using the Main() Function


This is a C program to display function without using the main function.

Problem Description

This program displays function without using the main function.

Problem Solution

1. Use #define macro to rename the main.
2. Print the output and exit.

Program/Source Code

Here is source code of the C Program to display function without using the main function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to display function without using the Main Function
  3.  */
  4. #include <stdio.h>
  5. #define decode(s,t,u,m,p,e,d) m##s##u##t
  6. #define begin decode(a,n,i,m,a,t,e)
  8. int begin()
  9. {
  10.     printf(" helloworld ");
  11. }
Program Explanation

1. Use #define macro for doing this.
2. #define decode(s,t,u,m,p,e,d) m##s##u##t.
This code says, next time when decode is encountered with 7 parameters, replace it by the concatenation of the 4th,1st,3rd and 2nd argument.
3. #define begin decode(a,n,i,m,a,t,e). In this line the 4th, 1st, 3rd and 2nd argument are m, a, i and n respectively. So the main is replaced by begin.
4. Use function named begin and print the output as ” helloworld ” and exit.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.