main

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

感情迁移 提交于 2019-11-28 17:06:23
问题 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() ? 回答1: 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

Why is argc not a constant?

廉价感情. 提交于 2019-11-28 16:55:36
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? 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 can't be made const for that reason also. (Aside: Interestingly, although getopt 's prototype suggests it won

Is ‘int main;’ a valid C/C++ program?

末鹿安然 提交于 2019-11-28 16:54:00
问题 I ask because my compiler seems to think so, even though I don’t. echo 'int main;' | cc -x c - -Wall echo 'int main;' | c++ -x c++ - -Wall Clang issues no warning or error with this, and gcc issues only the meek warning: 'main' is usually a function [-Wmain] , but only when compiled as C. Specifying a -std= doesn’t seem to matter. Otherwise, it compiles and links fine. But on execution, it terminates immediately with SIGBUS (for me). Reading through the (excellent) answers at What should main

`if __name__ == '__main__'` equivalent in Ruby

有些话、适合烂在心里 提交于 2019-11-28 15:56:10
I am new to Ruby. I'm looking to import functions from a module that contains a tool I want to continue using separately. In Python I would simply do this: def a(): ... def b(): ... if __name__ == '__main__': a() b() This allows me to run the program or import it as a module to use a() and/or b() separately. What's the equivalent paradigm in Ruby? From the Ruby I've seen out in the wild (granted, not a ton), this is not a standard Ruby design pattern. Modules and scripts are supposed to stay separate, so I wouldn't be surprised if there isn't really a good, clean way of doing this. EDIT: Found

C++ - char** argv vs. char* argv[]

孤街醉人 提交于 2019-11-28 15:39:17
What is the difference between char** argv and char* argv[] ? in int main(int argc, char** argv) and int main(int argc, char* argv[]) ? Are they the same? Especially that the first part does not have [] . Fred Foo They are entirely equivalent. char *argv[] must be read as array of pointers to char and an array argument is demoted to a pointer, so pointer to pointer to char , or char ** . This is the same in C . James Bedford They are indeed exactly the same. The golden rule of arrays to remember is: "The name of an array is a pointer to the first element of the array." So if you declare the

Compile and run program without main() in C

℡╲_俬逩灬. 提交于 2019-11-28 15:02:42
问题 I'm trying to compile and run following program without main() function in C . I have compiled my program using the following command. gcc -nostartfiles nomain.c And compiler gives warning /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400340 Ok, No problem. then, I have run executable file(a.out), both printf statements print successfully, and then get segmentation fault . So, my question is, Why segmentation fault after successfully execute print statements?

How do I swap two integers in an array, where my method takes in two integers and an array from main?

喜欢而已 提交于 2019-11-28 14:48:50
I call my swap method in main, but it doesn't change anything. What am I doing wrong? public static void main(String[] args){ int mainArr[] = new int[20]; for(int i = 0; i<mainArr.length; i++){ swapper(3, 14, mainArr); System.out.print(i + mainArr[i] + " "); } } public static void swapper (int a, int b, int[] mainArr){ int t = mainArr[a]; mainArr[a] = mainArr[b]; mainArr[b] = t; } My code yields 0, 1, 2, 3,...19 in normal ascending order, where I want it to swap the 4th and 15th element. Move the method call: - swapper(3, 14, mainArr); outside your for loop. Since, if your loop runs even

“int main (vooid)”? How does that work?

拜拜、爱过 提交于 2019-11-28 14:40:27
问题 I recently had to type in a small C test program and, in the process, I made a spelling mistake in the main function by accidentally using vooid instead of void . And yet it still worked. Reducing it down to its smallest complete version, I ended up with: int main (vooid) { return 42; } This does indeed compile ( gcc -Wall -o myprog myprog.c ) and, when run, it returns 42. How exactly is this valid code? Here's a transcript cut and pasted from my bash shell to show what I'm doing: pax$ cat qq

Issue with main arguments handling

℡╲_俬逩灬. 提交于 2019-11-28 14:34:28
I can't compare main() arguments with const char* strings. Simple code for explaining: #include <stdio.h> int main(int argc, char *argv[]) { int i; if(argc>1) { for (i=1;i<argc;++i) { printf("arg[%d] is %s\n",i,argv[i]); if(argv[i]=="hello") printf(" arg[%d]==\"hello\"\n",i); else printf(" arg[%d]!=\"hello\"\n",i); } } return 0; } Simple compile g++ test.cpp . When I try execute it, I see next thing: >./a.out hello my friend arg[1] is hello arg[1]!="hello" arg[2] is my arg[2]!="hello" arg[3] is friend arg[3]!="hello" Whats wrong with my code? In this statement if(argv[i]=="hello") you compare

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

柔情痞子 提交于 2019-11-28 14:12:45
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 with some randomly generated data, if 2, SalariedEmployee object with some random data and so on. I