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

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.

Comments
Good answer, but late.
1 year too late!! and useless post!
insipid

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 ;)

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

Comments
schmuck.

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.

This article has been dead for over six months. Start a new discussion instead.