main

Can argc be zero on a POSIX system?

孤街醉人 提交于 2019-12-03 01:58:18
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? Yes, it is possible. If you call your program as follows: execl("./myprog", NULL, (char *)NULL); Or alternately: char *args[] = { NULL }; execv("./myprog", args); Then in "myprog", argc will be 0. The standard also specifically allows for a 0 argc as noted in section 5.1.2.2.1 regarding program startup in a hosted environment: 1 The function called at program startup is named main . The implementation declares no prototype for this function.

Is main a valid Java identifier?

ぐ巨炮叔叔 提交于 2019-12-03 01:06:53
问题 One of my kids is taking Java in high school and had this on one of his tests: Which of the following is a valid identifier in Java? a. 123java b. main c. java1234 d. {abce e. )whoot He answered b and got it wrong. I looked at the question and argued that main is a valid identifier and that it should have been right. We took a look at the Java spec for identifiers and it reinforced that point. We also wrote a sample program that had a variable called main , as well as a method. He created a

int main(int argc, char** argv) [duplicate]

假如想象 提交于 2019-12-02 23:44:54
This question already has answers here : What is the proper declaration of main? (5 answers) Duplicate: What is the proper declaration of main? What do we mean by the arguments in this main function? What are they trying to tell us? int main(int argc, char** argv) UPDATE: And, is the preceding line of code similar to this int main(int argc, char* argv[]) ? If so, how can we say that char** argv is similar to char* argv[] as they don't look similar from an array point of view? How is it compared with int main() which does not have any arguments? Thanks. trojanfoe The argc parameter is the

Using module's own objects in __main__.py

跟風遠走 提交于 2019-12-02 20:02:15
I’m trying to access a module’s data from inside its __main__.py . The structure is as follows: mymod/ __init__.py __main__.py Now, if I expose a variable in __init__.py like this: __all__ = ['foo'] foo = {'bar': 'baz'} How can I access foo from __main__.py ? pdemb You need to either have the package already in sys.path , add the directory containing mymod to sys.path in __main__.py , or use the -m switch. To add mymod to the path would look something like this (in __main__.py ): import sys import os path = os.path.dirname(sys.modules[__name__].__file__) path = os.path.join(path, '..') sys

public static void main () access non static variable

萝らか妹 提交于 2019-12-02 19:33:50
Its said that non-static variables cannot be used in a static method.But public static void main does.How is that? No, it doesn't. public class A { int a = 2; public static void main(String[] args) { System.out.println(a); // won't compile!! } } but public class A { static int a = 2; public static void main(String[] args) { System.out.println(a); // this works! } } or if you instantiate A public class A { int a = 2; public static void main(String[] args) { A myA = new A(); System.out.println(myA.a); // this works too! } } Also public class A { public static void main(String[] args) { int a = 2

C++: Easiest way to access main variable from function?

瘦欲@ 提交于 2019-12-02 18:43:06
问题 I'm currently dealing with a string initialized in main() that for some reason freaks out (it becomes a string of non-characters) if I try and make it global. I'm wondering whether I can have a function declared in the program access this variable... the function itself will only ever be executed in main or functions called from main, but the compiler doesn't see the variable name until main() is reached. Here is a skinny version of the relevant code: string getCommand(int input_pos,string

Get name of executable jar from within main() method [duplicate]

*爱你&永不变心* 提交于 2019-12-02 18:00:00
This question already has an answer here: How to get the path of a running JAR file? 28 answers I've created an executable jar and using commons-cli to give the user the ability to specify command line parameters when he launches the client. Everything works fine. However, when I print the usage statement for the jar, I would like to show the following: usage: java -jar myprog.jar <options> <file> --help Display the help message --debug Enable debugging .... Printing of all the options is easily done with commons-cli. However, the "usage" line is the head scratcher. I cannot seem to figure out

Why does declaring main as an array compile?

守給你的承諾、 提交于 2019-12-02 17:50:48
I saw a snippet of code on CodeGolf that's intended as a compiler bomb, where main is declared as a huge array. I tried the following (non-bomb) version: int main[1] = { 0 }; It seems to compile fine under Clang and with only a warning under GCC: warning: 'main' is usually a function [-Wmain] The resulting binary is, of course, garbage. But why does it compile at all? Is it even allowed by the C specification? The section that I think is relevant says: 5.1.2.2.1 Program startup The function called at program startup is named main. The implementation declares no prototype for this function. It

PHP equivalent of Python's __name__ == “__main__”?

徘徊边缘 提交于 2019-12-02 17:28:44
As per the title, is there PHP equivalent of __name__ == "__main__" ? Is there something that would work for both scripts executed through the command line and through a web request, or would a custom function be needed? For those unfamiliar with Python, __name__ == "__main__" allows you to define a module file, and also have some things that allow you to run it if it is the entry point. The equivalent structure in PHP would resemble this: // SomeClass.php <?php class SomeClass { function doStuff() { echo "wahey!\n"; } } // python, I know. if (__name__ == "__main__") { $sc = new SomeClass; $sc

How does this C program compile and run with two main functions?

独自空忆成欢 提交于 2019-12-02 16:28:41
Today, while working with one custom library, I found a strange behavior. A static library code contained a debug main() function. It wasn't inside a #define flag. So it is present in library also. And it is used link to another program which contained the real main() . When both of them are linked together, the linker didn't throw a multiple declaration error for main() . I was wondering how this could happen. To make it simple, I have created a sample program which simulated the same behavior: $ cat prog.c #include <stdio.h> int main() { printf("Main in prog.c\n"); } $ cat static.c #include