11
Contributors
20
Replies
24
Views
9 Years
Discussion Span
Last Post by Narue
0

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

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.

0

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

0

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

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.

0

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.

0

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

-3

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.

Votes + Comments
Good answer, but late.
1 year too late!! and useless post!
insipid
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???

0

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

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

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.

0

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

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.

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

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.

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

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

-1

>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

Votes + Comments
schmuck.
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.