Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
How to write a running C code without main()?
Next article icon

How to write a running C code without main()?

Last Updated : 11 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C language code that prints GeeksforGeeks without any main function. Logically it's seems impossible to write a C program without using a main() function. Since every program must have a main() function because:-

  • It's an entry point of every C/C++ program.
  • All Predefined and User-defined Functions are called directly or indirectly through the main.

Therefore we will use preprocessor(a program which processes the source code before compilation) directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function. Let's see how the preprocessor doing their job:-  

Hence it can be solved in following ways:-up

  1. Using a macro that defines main 
C
#include<stdio.h>  #define fun main  int fun(void)  {      printf("Geeksforgeeks");      return 0;  } 
Output: Geeksforgeeks

         2. Using Token-Pasting Operator The above solution has word 'main' in it. If we are not allowed to even write main, we can use token-pasting operator (see this for details) 

C
#include<stdio.h>  #define fun m##a##i##n  int fun()  {      printf("Geeksforgeeks");      return 0;  } 
Output: Geeksforgeeks

        3. Using Argumented Macro 

C
#include<stdio.h>  #define begin(m,a,i,n) m##a##i##n  #define start begin(m,a,i,n)   void start() {  printf("Geeksforgeeks");  }  
Output: Geeksforgeeks

       4. Modify the entry point during compilation 

C
#include<stdio.h>  #include<stdlib.h>   // entry point function  int nomain();   void _start(){       // calling entry point      nomain();      exit(0);  }   int nomain()  {      puts("Geeksforgeeks");      return 0;  }  
Output:   Geeksforgeeks
Compilation using command : gcc filename.c -nostartfiles (nostartfiles option tells the compiler to avoid standard linking) Explanation: Under normal compilation the body of _start() will contain a function call to main() [ this _start() will be appended to every code during normal compilation], so if that main() definition is not present it will result in error like "In function `_start': (.text+0x20): undefined reference to `main'. In the above code what we have done is that we have defined our own _start() and defined our own entry point i.e nomain()

Refer Executing main() in C – behind the scene for another solution. References: Macros and Preprocessors in C 

Time Complexity: O(1)

Auxiliary Space: O(1)


Next Article
How to write a running C code without main()?

A

Abhay Rathi
Improve
Article Tags :
  • C Language
  • c-puzzle

Similar Reads

    Print "Hello World" in C/C++ without using any header file
    Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can
    2 min read
    How to Change the Output of printf() in main() in C?
    To change the output of printf() in main(), we can use Macro Arguments. #define macro can be used for this task. This macro is defined inside the function. Although, #define can be used without declaring it in the function, in that case always the printf() will be changed. The function needs to be c
    2 min read
    How to write your own header file in C?
    As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar
    4 min read
    What does main() return in C and C++?
    C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our t
    3 min read
    How to print "GeeksforGeeks" with empty main() in C, C++ and Java?
    Write a program that prints "GeeksforGeeks" with empty main() function. You are not allowed to write anything in main(). C language One way of doing this is to apply GCC constructor attribute to a function so that it executes before main() (See this for details). C #include <stdio.h> /* Apply
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences