main

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

血红的双手。 提交于 2019-11-30 07:47:30
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 main(int n_of_args, char *args[]); In the context of the compiler, everything is fine. These variables are local for main function, so they may have any names. And the simple code builds and runs perfectly. But these names may be used by preprocessor. So is it safe to rename these arguments? PS Personally I find these names bad, because they look

Why would you precede the main() function in C with a data type? [duplicate]

为君一笑 提交于 2019-11-30 06:52:57
This question already has an answer here: What should main() return in C and C++? 17 answers Many are familiar with the hello world program in C: #include <stdio.h> main () { printf ("hello world"); return 0; } Why do some precede the main() function with int as in: int main() Also, I've seen the word void entered inside the () as in: int main(void) It seems like extra typing for nothing, but maybe it's a best practice that pays dividends in other situations? Also, why precede main() with an int if you're returning a character string? If anything, one would expect: char main(void) I'm also

function try catch syntax and main

混江龙づ霸主 提交于 2019-11-30 06:45:59
问题 A little known, but almost never used C++ feature is given a declaration: void foo(); One possible, legal definition could be: void foo() try { throw 42; } catch(...) { } Here the whole function implementation wrapped is within a try/catch pair, which seems to be similar to allowing this. Is that legal to do for int main() ? E.g.: int main() try { throw 42; } catch(...) { } The rules for main, n3290 § 3.6.1 mostly talk about what arguments it should take and what it returns - they don't seem

javac: 错误:找不到或无法加载主类 com.sun.tools.javac.Main

余生长醉 提交于 2019-11-30 06:12:07
安装java jdk 有个基本常识 就是安装后要配置 java_home ,path和classpath, 而且安装路径不能有 空格。方法网上多的是,这里变不加赘述。 我在安装的时候出现了一个很难发现的问题(— -)——如题所述。在网上找了 很久,重新设置了很多次路径,电脑也重启了,就是没用。 原因:jdk和jre安装在同一个文件夹,jre覆盖了jdk的jre文件夹(总共两个哦)。 导致jre文件夹消失了一个!!所以安装java的时候要注意选择路径。或者干脆 使用默认路径。 然后path就都不用设置了,因为已经设置过了,除非你么设置或者换了文件夹安装了。 来源: oschina 链接: https://my.oschina.net/u/264056/blog/79374

Java: Calling a static method in the main() method

左心房为你撑大大i 提交于 2019-11-30 06:03:58
问题 I am supposed to do the following: Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object

Purpose of 'if __name__ == “__main__”:' [duplicate]

感情迁移 提交于 2019-11-30 05:31:22
This question already has an answer here: What does if __name__ == “__main__”: do? 29 answers I am trying to understand some code I found which reads command line arguments (attached below). My concern is what purpose of the "if __name__ == __main__" line is... Why would I use that line instead of just using the code below, main(sys.argv[1:]) . What extra use does it provide? import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i <inputfile> -o <outputfile>' sys.exit(2) for

Multiple main() methods in java

萝らか妹 提交于 2019-11-30 05:15:57
I was wondering what the effect of creating extra main methods would do to your code. For example, public class TestClass { public static void main (String[] args){ TestClass foo = new TestClass(); } } After the program initially starts up, foo will be created and it would have another public main method inside it. Will that cause any errors? It will cause no errors. Just because you initialize an object, doesn't mean the main method gets executed. Java will only initially call the main method of the class passed to it, like >java TestClass However , doing something like: public class

Why are recursive main() calls not allowed in C++? [duplicate]

纵然是瞬间 提交于 2019-11-30 03:53:20
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: restrictions on the main() function Is it legal to recurse into main() in C++? I read in C++ Primer that main is not allowed to be called recursively, and in some related questions here on SO it is indeed confirmed that it is illegal. But why is it illegal? As long as you avoid a stack overflow, what's the problem with calling main within itself? 回答1: Well, the standard states: 3.6.1.3 "The function main shall

Multiple Main Functions

一世执手 提交于 2019-11-30 02:35:03
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[] args) { // Some Code } } Now I understand that main is often the "entry point" when you run the program. So basically, your program will start executing wherever the main function is. But I believe in both languages you can have multiple main functions within multiple classes. So when I compile a project with multiple main functions, where is the "entry

Is it possible to write a program without using main() function?

旧时模样 提交于 2019-11-29 20:56:24
I keep getting this question asked in interviews: Write a program without using main() function? One of my friends showed me some code using Macros, but i could not understand it. So the question is: Is it really possible to write and compile a program without main() ? Cheers and hth. - Alf Within standard C++ a main function is required, so the question does not make sense for standard C++. Outside of standard C++ you can for example write a Windows specific program and use one of Microsoft's custom startup functions (wMain, winMain, wWinmain). In Windows you can also write the program as a