main

Why main() cannot be declared as a static in C?

a 夏天 提交于 2019-11-28 23:15:28
问题 Why main must be declared as if it has external linkage? Why it should not be static? what is meant by external linkage?? 回答1: Because you link the startup files to your program, which contains (usually) assembler code that calls your main. If main were static, that code wouldn't be able to call main. external linkage means that other so-called translation-units can see your symbol declared extern in its own translation-unit. So, your main is extern, and it will have an entry in its

Running a python package [duplicate]

我只是一个虾纸丫 提交于 2019-11-28 23:07:32
This question already has an answer here: Execute an installed Python package as a script? 3 answers Running Python 2.6.1 on OSX, will deploy to CentOS. Would like to have a package to be invoked from a command line like this: python [-m] tst For that, here is the directory structure made: $PYTHONPATH/ tst/ __init__.py # empty __main__.py # below dep.py # below The following is in the files: $ cat tst/__main__.py from .dep import DepClass print "Hello there" $ cat tst/dep.py class DepClass(object): pass $ However, python gives me conflicting diagnostic: $ python -m tst /usr/bin/python: tst is

calling another method from the main method in java [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-28 22:57:00
This question already has an answer here: calling non-static method in static method in Java [duplicate] 14 answers I have class foo{ public static void main(String[] args){ do(); } public void do(){} } but then when I call do() from main by running the command java foo on the command line, java complains that you can't call a method from a static function. So my question is: How do you call methods from the main method and if it is not possible what are some alternative strategies to call methods after the program is run from the command line using the java command. You can only call instance

Why does const int main = 195 result in a working program but without the const it ends in a segmentation fault?

旧街凉风 提交于 2019-11-28 22:34:30
Consider following C program (see live demo here ). const int main = 195; I know that in the real world no programmer writes code like this, because it serves no useful purpose and doesn't make any sense. But when I remove the const keyword from above the program it immediately results in a segmentation fault . Why? I am eager to know the reason behind this. GCC 4.8.2 gives following warning when compiling it. warning: 'main' is usually a function [-Wmain] const int main = 195; ^ Why does the presence and absence of const keyword make a difference here in the behavior of the program? Observe

How can I write a Windows application without using WinMain?

南笙酒味 提交于 2019-11-28 21:27:42
Windows GUI applications written in C/C++ have 'WinMain' as an entry point (rather than 'main'). My understanding of this is that the compiler generates a 'main' function to be called by the C Runtime. This 'main' function sets up the necessary environment for the GUI and calls into 'WinMain' (specifying the instance handles etc.). In short, I believe console and GUI application startup to differ in the following way: Console application: C Runtime --> 'main' function (hand-coded) GUI application: C Runtime --> 'main' function (compiler-generated) --> 'WinMain' function (hand-coded) I would

function try catch syntax and main

蹲街弑〆低调 提交于 2019-11-28 21:24:49
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 to explicitly forbid it as they do with various other odd things (e.g. linkages) you might be

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

十年热恋 提交于 2019-11-28 20:00:13
I was reading the C++ FAQs and I noticed one sentence. main() cannot be inline. Why is this? 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. 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: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ] And why does it

C Main Loop without 100% cpu

橙三吉。 提交于 2019-11-28 19:25:01
#include <stdio.h> int main() { while(!DONE) { /* check for stuff */ } return 0; } The above code sample uses 100% cpu until DONE is true. How can I implement a program that loops and only terminates when DONE, but which doesn't use 100% cpu? Modern languages use something like App.ProcessMessages or something like that to give the OS the control for the moment and then return to the loop. I'm new at C, obviously... using latest GCC, linux and windows (a portable solution would be great!) It depends what you want to do inside this loop. If you are waiting inside the loop (i.e. if keypressed {

main() function in C

早过忘川 提交于 2019-11-28 19:16:15
I've been learning C programming in a self-taught fashion for some weeks, and there are some questions that I have concerning the main() function. All functions must be declared in their function prototype, and later on, in their defintion. Why don't we have to declare the main() function in a prototype first? Why do we have to use int main() instead of void main() ? What does return 0 exactly do in the main() function? What would happen if I wrote a program ending the main() function with return 1; , for example? Fred Foo A declaration of a function is needed only before a function is used.

Why main does not return 0 here?

只愿长相守 提交于 2019-11-28 17:20:43
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); } int main() { int a=10; int b=5; int ans; ans=sum(a,b); printf("sum is %d",ans); } compile gcc test.c ./a