Questions tagged [program-entry-point]

Use this tag for questions about entry points to executables (programs); e.g., the "main()" function in most programming languages. If your question is programming language specific, tag that: language as well e.g. C, C++, Java etc.

2997 questions
114
votes
9 answers

Is ‘int main;’ a valid C/C++ program?

I ask because my compiler seems to think so, even though I don’t. echo 'int main;' | cc -x c - -Wall echo 'int main;' | c++ -x c++ - -Wall Clang issues no warning or error with this, and gcc issues only the meek warning: 'main' is usually a function…
Geoff Nixon
  • 4,697
  • 2
  • 28
  • 34
114
votes
3 answers

`if __name__ == '__main__'` equivalent in Ruby

I am new to Ruby. I'm looking to import functions from a module that contains a tool I want to continue using separately. In Python I would simply do this: def a(): ... def b(): ... if __name__ == '__main__': a() b() This allows…
Imagist
  • 18,086
  • 12
  • 58
  • 77
108
votes
7 answers

Why is argc not a constant?

int main( const int argc , const char[] const argv) As Effective C++ Item#3 states "Use const whenever possible", I start thinking "why not make these 'constant' parameters const"?. Is there any scenario in which the value of argc is modified in a…
Dinushan
  • 2,067
  • 6
  • 30
  • 47
98
votes
7 answers

How can a program with a global variable called main instead of a main function work?

Consider following program: #include int main = ( std::cout << "C++ is excellent!\n", 195 ); Using g++ 4.8.1 (mingw64) on Windows 7 OS, the program compiles and runs fine, printing: C++ is excellent! to the console. main appears to be…
Destructor
  • 14,123
  • 11
  • 61
  • 126
94
votes
8 answers

What is the Eclipse shortcut for "public static void main(String args[])"?

I know a cool shortcut for System.out.println(): sysout Ctrl + Space. Is there something similar for public static void main(String args[])?
user244333
92
votes
6 answers

Can argc be zero on a POSIX system?

Given the standard definition for the main program: int main(int argc, char *argv[]) { ... } Under which circumstances can argc be zero on a POSIX system?
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
85
votes
4 answers

Is char *envp[] as a third argument to main() portable

In order to get an environment variable in a C program, one could use the following: getenv() extern char **environ; But other than the above mentioned, is using char *envp[] as a third argument to main() to get the environment variables…
84
votes
10 answers

Is it safe to rename argc and argv in main function?

A lot of programs use standard names for a number of arguments and arrays of strings. The prototype of main function looks like: int main(int argc, char *argv[]);. But would I break something if I choose custom names for these variables? E.g. int…
yanpas
  • 2,155
  • 1
  • 17
  • 26
82
votes
7 answers

WINMAIN and main() in C++ (Extended)

Right, I have looked at this post: Difference between WinMain,main and DllMain in C++ I now know that WINMAIN is used for window applications and main() for consoles. But reading the post doesn't really tell me why exactly what is the difference. I…
Danny
  • 9,199
  • 16
  • 53
  • 75
80
votes
2 answers

Compile and run program without main() in C

I'm trying to compile and run following program without main() function in C. I have compiled my program using the following command. gcc -nostartfiles nomain.c And compiler gives warning /usr/bin/ld: warning: cannot find entry symbol _start;…
msc
  • 33,420
  • 29
  • 119
  • 214
74
votes
3 answers

How to access global variables

How do I access a variable that was declared/init in my main.go in a different .go package/file? Keeps telling me that the variable is undefined (I know that global variables are bad but this is just to be used as a timestamp) in main.go var…
Nighthee
  • 1,329
  • 3
  • 13
  • 14
74
votes
10 answers

Multiple Main Functions

I'm a bit new at this so bear with me. I'm currently learning C# and Java and one of their similarities is that the main function needs to be encapsulated within a class. For example public class HelloWorld { public static void main(String[]…
Ben
  • 1,233
  • 2
  • 14
  • 17
73
votes
5 answers

Using Spring 3 autowire in a standalone Java application

Here is my code: public class Main { public static void main(String[] args) { Main p = new Main(); p.start(args); } @Autowired private MyBean myBean; private void start(String[] args) { …
71
votes
22 answers

Could not find or load main class with a Jar File

I'm trying to load a jar using @echo off java -jar Test.jar pause With the manifest of Manifest-Version: 1.0 Main-Class: classes.TestClass In the Jar directory, I can clearly see a classes\TestClass file when I extract it. Edit: classes.TestClass…
Austin
  • 4,801
  • 6
  • 34
  • 54
70
votes
17 answers

Why main() in C++ cannot be inlined?

I was reading the C++ FAQs and I noticed one sentence. main() cannot be inline. Why is this?
jon
  • 615
  • 5
  • 4