Is it possible to print anything on screen in C or C++ without using main().If yes please explain how?

Dani AI

Generated

Short answer: under the hosted ISO C/C++ model a program is required to supply a main entry point. In practice, though, several platform- or compiler-specific mechanisms let code run (and print) before or instead of the user-written main. That explains the mixed replies here: pointed out the standard rule, hinted at the Windows alternate entry, and / showed how C++ static initialization can be used to produce output with an empty main.

Common practical approaches (portable to varying degrees):

  • Platform entry points: GUI apps on Windows use a different entry symbol so console main is not used; that can produce visible behavior but may not show a console by default.
  • C++ static initialization: a global object constructor runs before main, so it can emit output even when main is empty. This is what many contest "trick" answers exploit.
  • Compiler/linker features: GCC/clang support __attribute__((constructor)) to run a function before main. At the lowest level, providing your own _start (or using the system start-up symbol) lets a program call syscalls directly and avoid C runtime main.

Example (GCC/clang trick; not shown earlier in the thread):

#include <cstdio>

__attribute__((constructor))
static void pre_main(void) {
    puts("Hello from before main");
}

int main(void) { return 0; }

Important cautions: static constructors and start-up hooks are non-portable and can interact poorly with library initialization (iostreams in particular may not be reliably ready during static init across translation units). Judges or build systems for contests may enforce hosted rules or linker defaults that reject missing/alternate entry points, and Windows GUI vs console builds affect whether console output is visible. For a contest puzzle, state the environment and approach used; for production code, follow the standard and use main.

Recommended Answers

All 20 Replies

main() is the start of any C++ application, why do you need to do it without it ?

main() is the start of any C++ application, why do you need to do it without it ?

In one of the programming competetions in my college we were asked to do this.
Although none was successful

Because it's a frequent teaser question which some tutors think are useful in some way.
Like for example printing without using a ; or making the program print it's source code or some other dumb trick.

>>Is it possible to print anything on screen in C or C++ without using main().

Yes -- write a MS-Windows win32 api program which uses WinMain() instead of main(). Or use VC++ 2008 and it will generate a console program that starts with _tmain().

Thanks very much for your reply.But please I need some more help or atleast some hints as I dont know how to create a MS-Windows win32 api program which uses WinMain() instead of main(). I am new at programming . So please help

>So please help
Try searching for WinMain on www.google.com. Seriously, it's not that hard to do a little bit of research and come up with something. We tend not to take kindly to willfully clueless people.

Every ISO standard C/C++ program must have a main(). Windows, other graphic environments, and many embedded systems have a non-standard environment and may not need a main().

So, the answer is: in ISO Standard C/C++, you cannot print anything without having a main() function. If you are talking about a non-standard environment, then the answer is it depends on the environment.

>So, the answer is: in ISO Standard C/C++, you cannot
>print anything without having a main() function.
Unless you're on a freestanding implementation.

Ok the question asked is never print anything without main() function. The question asked is without anything in main function and it is possible(I know it because i myself have asked this question many times). You can do just about anything without anything inside main() function. The answer is C++ -> class -> global object. Try it and you'll see.

commented: insipid -2
commented: 1 year too late!! and useless post! -1
commented: Good answer, but late. -2

Hhm, what about with a class? Wait ... no, that won't help. Main is like the major function in a program in C++, I don't know if you can, have you checked in a C++ book? I think it should have it ... What if you make a function that acts like main but without using main???

abhishekp is right, but not class only and not global only:

int main() {} // do nothing!!!
static struct __
{
     __() { cout << "Hello..."; }
    ~__() { cout << "Bye!\n"; }
}
_;

You could hard code int main(){} as char main[] { /*opcodes*/ } (with extern "C" in VC++).
So technically it would then be main[] not main() anymore.

I think it's impossible to solve undefined problem...
hosted or freestanding implementation... without main - with WinMain - with exotic compilers...
Nonsense ;)

Oh, true!

Hhm, what about with a class? Wait ... no, that won't help. Main is like the major function in a program in C++, I don't know if you can, have you checked in a C++ book? I think it should have it ... What if you make a function that acts like main but without using main???

What i meant was the question will be like. Write a program in c or c++ to print "hello" without any statements in main() function and it is possible.

#include<iostream>
class hello
{
public:
hello(){ cout << "Hello"; };
};

hello wrld;

int main(){}

The code may not compile right away, I wrote it in a hurry, it may need some editing.

>...and it is possible.
Now compare two problem definitions:
OP: >Is it possible to print anything on screen in C or C++ without using main().
#2: >Write a program in c or c++ to print "hello" without any statements in main() function.
Feel the difference.
;)

>So please help
Try searching for WinMain on www.google.com. Seriously, it's not that hard to do a little bit of research and come up with something. We tend not to take kindly to willfully clueless people.

Great Help!!!... sike, be a little more of a bitch next time

>Great Help!!!... sike, be a little more of a bitch next time
You're not looking any better, smart-ass. I love how those most vocal about me not being nice are so hypocritical. :)

>So please help
Try searching for WinMain on www.google.com. Seriously, it's not that hard to do a little bit of research and come up with something. We tend not to take kindly to willfully clueless people.

>Great Help!!!... sike, be a little more of a bitch next time
You're not looking any better, smart-ass. I love how those most vocal about me not being nice are so hypocritical. :)

I have this thing with not liking people who think there better than everyone else. dont disrespect people that are trying to learn

commented: schmuck. -2

Why don't you help people instead of trying to defend their honor? It's far more productive than making an ass of yourself on your first day.

>I have this thing with not liking people who think there better than everyone else.
I have this thing about not giving a damn what other people think of me. :yawn:

>dont disrespect people that are trying to learn
Respect is earned.

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.