954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Program without main()

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

rukunaditya
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 
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

rukunaditya
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

>>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().

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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

rukunaditya
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

eager
Newbie Poster
12 posts since Jan 2008
Reputation Points: 18
Solved Threads: 2
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

abhishekp
Newbie Poster
3 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

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???

MariaERamos
Newbie Poster
10 posts since May 2009
Reputation Points: 17
Solved Threads: 0
 

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

int main() {} // do nothing!!!
static struct __
{
     __() { cout << "Hello..."; }
    ~__() { cout << "Bye!\n"; }
}
_;
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Oh, true!

MariaERamos
Newbie Poster
10 posts since May 2009
Reputation Points: 17
Solved Threads: 0
 
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.

abhishekp
Newbie Poster
3 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
>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

jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

>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. :)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>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

jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You