cpp without using main function

Hi guys,

I want to have the same output, but without using the main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <locale.h>

using namespace std;

char * format(double d, char *buf) {
  char *loc = setlocale(LC_ALL, NULL);
  setlocale(LC_ALL, "nl_NL.utf-8");
  sprintf(buf, "%.2f", d);
  loc = setlocale(LC_ALL, loc); // restore locale
  return buf;
}

int main(int argc, char** argv) {
  if (argc != 2 || atof(argv[1]) == 0.0) {
    cerr << "Usage: " << argv[0] << " <number>\n";
    return 1;

  } else {
    char formatted_value[255];
    cout << format(atof(argv[1]), formatted_value) << "\n";
    return 0;
  }
}


Is that possible? How will it look like?
No, that's not possible. The CPU needs to have a place in your .exe it starts reading at, and in C++ that place is called main.
Use old versions of MSVC, it had a habit of using _tmain() instead of main()
Use old versions of MSVC, it had a habit of using _tmain() instead of main()


Who said he was using MSVC? He could be using a basic txt editor and compiling with gcc (g++ for c++), or using XCode like I do!
ajh32, I proposed him a method to go withoust main() function, because, you know, main() != _tmain()
ajh32, I proposed him a method to go withoust main() function, because, you know, main() != _tmain()


That could be an option, yet I'm looking for a more logical solution
@MiinNiPaa - I don't think that was what he was asking. From the sound of it he wanted to have his own function called at the start, i.e.

 
int MyOwnStartPoint()


Which obviously he cannot do.
Damn, I am in dire need of sarcasm tag on this forum.
Actually you can do it: download gcc sources, find where main function referenced, change it to whichever you need, compile and you have non-standard compliant compiler which use something else as entry point insted of main().
Last edited on
That could be an option, yet I'm looking for a more logical solution

What do you mean by that?

For that matter, what do you mean by your original question? Every program must have an entry point, and that entry point is the main function. Please could you be more specific about what you're trying to achieve here?
Last edited on

That could be an option, yet I'm looking for a more logical solution


What do you mean by that?

For that matter, what do you mean by your original question? Every program must have an entry point, and that entry point is the main function. Please could you be more specific about what you're trying to achieve here?


Thanks for your reply Mike. I'm using this code to replace a dot for a comma and sets 2 decimal places (a specific program needs to use this cpp as a routine). This is based on an entry point. My question; how does this code look like if it doesn't make use of an entry point? In other words, not making use of the main function.

Excuse me if I'm not clear enough, I'm quite new with CPP :)
Thanks for your replies MiiNiPaa and ajh32, please take a look to my previous comment. Thank you guys in advance

Thanks for your reply Mike. I'm using this code to replace a dot for a comma and sets 2 decimal places (a specific program needs to use this cpp as a routine). This is based on an entry point. My question; how does this code look like if it doesn't make use of an entry point? In other words, not making use of the main function.


I have no idea what you are trying to say! Can you please explain yourself, what are you attempting to do?
Topic archived. No new replies allowed.