main

Why does my C++ compiler allow recursive calls to main? [duplicate]

两盒软妹~` 提交于 2019-12-01 19:21:39
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is it legal to recurse into main() in C++? #include <iostream> using namespace std; int main() { static int var = 5; std::cout << --var; if(var) main(); } gcc compiles the code http://ideone.com/lIp3A . I know that main cannot be used inside main in C++. How come this code compiles? 回答1: The code is ill-formed because it violates the shall construct of §3.6.1.3 §3.6.1.3 says : The function main shall not be used

Why does my C++ compiler allow recursive calls to main? [duplicate]

*爱你&永不变心* 提交于 2019-12-01 18:51:48
Possible Duplicate: Is it legal to recurse into main() in C++? #include <iostream> using namespace std; int main() { static int var = 5; std::cout << --var; if(var) main(); } gcc compiles the code http://ideone.com/lIp3A . I know that main cannot be used inside main in C++. How come this code compiles? The code is ill-formed because it violates the shall construct of §3.6.1.3 §3.6.1.3 says : The function main shall not be used within a program. The shall construct A diagnosable rule is defined as (§1.4.1): The set of diagnosable rules consists of all syntactic and semantic rules in this

Two Main methods with different signatures

夙愿已清 提交于 2019-12-01 17:51:11
问题 I have following class. public class Test { public static void main(Integer[] args) { System.out.println("This is not a main"); } public static void main(String[] args) { System.out.println("This is the main"); } } In here there are two main method which are accept Integer[] and String [] as input argument. My question is how JVM always load second method as main method of this class. Why always consider input argument as array of String ? 回答1: Because that's what Java always looks for. Java

Is it illegal to take address of main() function?

不问归期 提交于 2019-12-01 16:57:55
问题 According to this answer using function main() is illegal (§3.6.1.3) and a function is used if its name appears in a potentially evaluated expression (§3.2). Suppose I have this code: printf( "%p", &main ); in which name of function main() appears in expression &main . Will the code above be illegal? 回答1: Yes. As you quote, the standard says that you cannot use main . Note too that the address of a function does not match "%p" . The corresponding argument must have type void* ; any other type

Is it illegal to take address of main() function?

限于喜欢 提交于 2019-12-01 16:56:33
According to this answer using function main() is illegal (§3.6.1.3) and a function is used if its name appears in a potentially evaluated expression (§3.2). Suppose I have this code: printf( "%p", &main ); in which name of function main() appears in expression &main . Will the code above be illegal? Yes. As you quote, the standard says that you cannot use main . Note too that the address of a function does not match "%p" . The corresponding argument must have type void* ; any other type (except maybe char* ) is illegal, and results in undefined behavior. Since main is not "used" (you're not

What's the point of String[] args in Java?

帅比萌擦擦* 提交于 2019-12-01 14:17:44
Whenever you declare the main method in a class, you always have to do a String array called "args". What's the point? Unless I live under a rock, command line agruments in Java are barely used anymore. And when I try and run this... //this program won't compile public class SomeClass { public static void main(){ System.out.println("This text will never be displayed :("); } } The output shows this in red text: Error: Main method not found in class SomeClass , please define the main method as: public static void main(String[] args) I, the newbie Java programmer, would greatly appreciate it if

Change Main in WPF project

邮差的信 提交于 2019-12-01 13:52:57
Always annoyed me how C# wants to do the startup for you. So now I am trying to make my own main method. It's not working: I have provided this main method: [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main() { Model model= new Model(); Controller controller = new Controller(model); MainWindow window = new MainWindow(controller, model); } This method is run, but I cant see anything visual. I think I miss something from the following normal main code: Application.App app = new Application.App(); app.InitializeComponent(); app.Run(); I have

Where does the returned value for 'main' function go?

落爺英雄遲暮 提交于 2019-12-01 12:00:01
In C, a function always returns its value to the calling function and never to itself ( if return type is not void ). Like, int main() But since ' main ' function is called by the Operating System , which is not a function . So, whom does the 'main' function returns its value? Where does the value go, when its returned using the expression return(0); at the end of the program? This is completely OS-specific, but usually the OS invokes a program by Setting up the program's address space, Creating a record of the new process somewhere in the OS internals, Launching its own custom function, which

Change Main in WPF project

岁酱吖の 提交于 2019-12-01 10:57:12
问题 Always annoyed me how C# wants to do the startup for you. So now I am trying to make my own main method. It's not working: I have provided this main method: [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main() { Model model= new Model(); Controller controller = new Controller(model); MainWindow window = new MainWindow(controller, model); } This method is run, but I cant see anything visual. I think I miss something from the following

Multiple classes in a single Java file, each with a main method - unexpected behavior?

橙三吉。 提交于 2019-12-01 08:30:53
I have got the following code in a file called test.java which is located inside the directory C:\D\JavaProjects class test { public static void main( String[] str ) { System.out.println( "Hello, World! from test" ); } } class Test { public static void main( String[] str ) { System.out.println( "Hello, World!" ); } } When I do "javac test.java" it outputs test.class. Now, if I do "java test" I get the following output: Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: Test) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond