main

in c++ main function is the entry point to program how i can change it to an other function?

*爱你&永不变心* 提交于 2019-11-27 13:07:38
I was asked an interview question to change the entry point of a C or C++ program from main() to any other function. How is it possible? In standard C (and, I believe, C++ as well), you can't, at least not for a hosted environment (but see below). The standard specifies that the starting point for the C code is main . The standard (c99) doesn't leave much scope for argument: 5.1.2.2.1 Program startup: (1) The function called at program startup is named main. That's it. It then waffles on a bit about parameters and return values but there's really no leeway there for changing the name. That's

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

回眸只為那壹抹淺笑 提交于 2019-11-27 12:37:17
问题 I was reading the C++ FAQs and I noticed one sentence. main() cannot be inline. Why is this? 回答1: In C++ it is not legal to call the main function in your code, so there'd be no way it could ever be inlined. 回答2: Because the standard says so: [2003: 3.6.1/3] : The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example:

Why does int main() {} compile?

淺唱寂寞╮ 提交于 2019-11-27 12:36:35
(I'm using Visual C++ 2008) I've always heard that main() is required to return an integer, but here I didn't put in return 0; and and it compiled with 0 errors and 0 warnings! In the debug window it says the program has exited with code 0. If this function is named anything other than main(), the compiler complains saying 'blah' must return a value. Sticking a return; also causes the error to appear. But leaving it out completely, it compiles just fine. #include <iostream> using namespace std; int main() { cout << "Hey look I'm supposed to return an int but I'm not gonna!\n"; } Could this be

Xcode 8 beta 6: main.swift won't compile

只愿长相守 提交于 2019-11-27 11:56:58
We have a custom UIApplication object, so our main.swift was import Foundation import UIKit UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(MobileUIApplication), NSStringFromClass(AppDelegate)) and that didn't work in Xcode 8 beta 5 so we used this //TODO Swift 3 workaround? https://forums.developer.apple.com/thread/46405 UIApplicationMain( Process.argc, UnsafeMutablePointer<UnsafeMutablePointer<CChar>>(Process.unsafeArgv), nil, NSStringFromClass(AppDelegate.self)) On Xcode 8 beta 6 we get Use of unresolved identifier 'Process' What do we need to do in Xcode 8 beta 6

Why main does not return 0 here?

我的梦境 提交于 2019-11-27 10:38:50
问题 I was just reading ISO/IEC 9899:201x Committee Draft — April 12, 2011 in which i found under 5.1.2.2.3 Program termination ..reaching the } that terminates the main function returns a value of 0. it means if you don't specify any return statement in main() , and if the program runs successfully, then at the closing brace } of main will return 0. But in the following code i don't specify any return statement, yet it does not return 0 #include<stdio.h> int sum(int a,int b) { return (a + b); }

Why is argc not a constant?

ぐ巨炮叔叔 提交于 2019-11-27 10:03:42
问题 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 program? 回答1: In this case, history is a factor. C defined these inputs as "not constant", and compatibility with (a good portion of) existing C code was an early goal of C++. Some UNIX APIs, such as getopt , actually do manipulate argv[] , so it

What if main method is inside “non public class” of java file?

半腔热情 提交于 2019-11-27 09:29:08
I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can't run that java file. Why is that? and there is no compilation error as well. If so, how can I use that main method? You can certainly override main method and it does not violate any compiler rules and hence you will not have any compiler errors. You check that inspite of the fact that you have more than one class a file that is declared as public is the name of the file you are trying to execute. This is a convention that the file should be named after the same

Prevent the main() function from terminating before goroutines finish in Golang

主宰稳场 提交于 2019-11-27 09:28:57
Have loook at this contrived example: package main import "fmt" func printElo() { fmt.Printf("Elo\n") } func printHello() { fmt.Printf("Hello\n") } func main() { fmt.Printf("This will print.") i := 0 for i < 10 { go printElo() go printHello() i++ } } The output of this program would be just "This will print". Output of goroutines printElo() and printHello will not be emitted because, I guess, the main() function thread will finish before the goroutines have a chance to even start executing. What is the idiomatic way to make similar code work in Golang and not terminate prematurely? Simplest,

legal main method signature in java

北城余情 提交于 2019-11-27 09:17:34
class NewClass{ public static void main(String a){ System.out.print("Hello"); } } When I'm trying to execute above code, then it shows an error, main method not found . But when I changed public static void main(String a) to public static void main(String... a) or public static void main(String a[]) . Then, it works..!! So, My question is how many different ways we can write legal main method signature and what this signature public static void main(String... a) means ? Simply because that's the requirement of Java. A main method/entry point to a program must be a method declared as public

How do Java programs run without defining the main method?

こ雲淡風輕ζ 提交于 2019-11-27 09:16:07
I was looking through some Java source and noticed that the main method wasn't defined. How does Java compile source code without knowing where to start? The main method is only used when the Java Virtual Machine is executing your code. Code cannot be executed without a main method but it can still be compiled. When compiling code, you usually specify a set of files on the command line e.g. javac MyClass1.java MyClass2.java The Java compiler ( javac ) examines each class you passed to it and compiles it into a .class file. One reason Java source code may be missing a main method is because it