EzDevInfo.com

main

Implementations of Python and Ruby programming languages for .NET Framework that are built on top of the Dynamic Language Runtime.

Why do we need argc while there is always a null at the end of argv?

It seems that the argv[argc] is always NULL, so I think we can traverse the argument list without argc. A single while loop will do this.

If there is always a NULL at the end of argv, why do we need an argc?


Source: (StackOverflow)

Where are C/C++ main function's parameters?

In C/C++, the main function receives parameters which is of type char*.

int main(int argc, char* argv[]){
  return 0;
}

argv is array of char*, they point to strings. Where do these string locate? Are they on heap, or stack, or somewhere else?


Source: (StackOverflow)

Advertisements

What is "main" in Ruby?

If I run this file as "ruby x.rb":

class X
end
x = X.new

What is the thing that is calling "X.new"?

Is it an object/process/etc?


Source: (StackOverflow)

Is it legal to recurse into main() in C++?

I read that the C++ standard forbids recursion in main(), but g++ compiles the following code without complaint:

int main()
{
    main();
}

Can anyone clarify this?


Source: (StackOverflow)

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

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()?


Source: (StackOverflow)

Should I return EXIT_SUCCESS or 0 from main()?

It's a simple question, but I keep seeing conflicting answers: should the main routine of a C++ program return 0 or EXIT_SUCCESS?

#include <cstdlib>
int main(){return EXIT_SUCCESS;}

or

int main(){return 0;}

Are they the exact same thing? Should EXIT_SUCCESS only be used with exit()?

I thought EXIT_SUCCESS would be a better option because other software may want to deem zero as failure, but I also heard that if you return 0, the compiler is capable of changing it to a different value anyway.


Source: (StackOverflow)

What does `if __name__ == "__main__":` do?

What does the if __name__ == "__main__": do?

# Threading example
import time, thread

def myfunction(string, sleeptime, lock, *args):
    while 1:
        lock.acquire()
        time.sleep(sleeptime)
        lock.release()
        time.sleep(sleeptime)
if __name__ == "__main__":
    lock = thread.allocate_lock()
    thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
    thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

Source: (StackOverflow)

Why is the Java main method static?

The method signature of a Java main() method is:

public static void main(String[] args){
    ...
}

Is there a reason for this method to be static?


Source: (StackOverflow)

Can't execute jar- file: "no main manifest attribute"

I have installed an application, when I try to run it (it's an executable jar) nothing happens. When I run it from the commandline with:

java -jar "app.jar"

I get the following message:

no main manifest attribute, in "app.jar"

Normally, if I had created the program myself, I would have added a main class attribute to the manifest file. But in this case, since the file is from an application, i cannot do that. I also tried extracting the jar to see if I could find the main class, but there are to many classes and none of them has the word "main" in it's name. There must be a way to fix this because the program runs fine on other systems.


Source: (StackOverflow)

"int main (vooid)"? How does that work?

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.c
int main (vooid) {
    return 42;
}

pax$ rm qq ; gcc -Wall -o qq qq.c ; ./qq

pax$ echo $?
42

Source: (StackOverflow)

Difference between int main() and int main(void)?

What does the following mean :

int main(void) {...} 

VS

int main() {...}

?

I think that int main() {...} means that main doesn't receive any parameters (from command line) , however:

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

does.

But what does int main(void) {...} means ? what does the void stands for ?

I've looked here but it's somehow a different question .


Source: (StackOverflow)

What does "Could not find or load main class" mean?

A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class ...

What does this mean, what causes it, and how should you fix it?


Source: (StackOverflow)

Is main() really start of a C++ program?

The section $3.6.1/1 from the C++ Standard reads,

A program shall contain a global function called main, which is the designated start of the program.

Now consider this code,

int square(int i) { return i*i; }
int user_main()
{ 
    for ( int i = 0 ; i < 10 ; ++i )
           std::cout << square(i) << endl;
    return 0;
}
int main_ret= user_main();
int main() 
{
        return main_ret;
}

This sample code does what I intend it to do, i.e printing the square of integers from 0 to 9, before entering into the main() function which is supposed to be the "start" of the program.

Have a look at the output here : http://www.ideone.com/Niy0R

I also compiled it with -pedantic option, GCC 4.5.0. It gives no error, not even warning!

So my question is,

Is this code really Standard conformant?

If it's standard conformant, then does it not invalidate what the Standard says? main() is not start of this program! user_main() executed before the main().

I understand that to initialize the global variable main_ret, the use_main() executes first but that is a different thing altogether; the point is that, it does invalidate the quoted statement $3.6.1/1 from the Standard, as main() is NOT the start of the program; it is in fact the end of this program!


EDIT:

How do you define the word 'start'?

It boils down to the definition of the phrase "start of the program". So how exactly do you define it?


Source: (StackOverflow)

Why main does not return 0 here?

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.out
sum is 15
echo $?
9          // here it should be 0 but it shows 9 why?

Source: (StackOverflow)