Is main with parameter list of void different from main with an empty parameter list?

36,211

Solution 1

In C, in general, (void) means no arguments required in function call, while () means unspecified number of arguments.

e.g.

void foo(void)
{
   // body
}

void bar()
{
    //body
}

In calling enviroment,

foo();  // Correct 
foo(1); // Incorrect
bar();  // Correct
bar(1); // Also correct

This was the general explanation.

But for your case for main() , C99 Standard says that,

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 shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ }

or

with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* ... */ } or equivalent;

or

in some other implementation-defined manner.

So, in this void main(void) return type should be int.

And at last , for main(), return type is not given so implicitly return type would be int.

Solution 2

Basically, void is a data type, which basically used with method declaration. It means nothing or no type. Eg:

1) int myFunc(void) -- the function takes nothing.

2) void myFunc(int) -- the function returns nothing

3) void* data; -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

Solution 3

Excluding the return type of the main as in

main(){
}

doesn't mean that it's a void type, it depends on the compiler. I think it can be said it's generically interpreted as

int main(){
}

The void type tells the compiler that there is no 'entity' (no 'storage'), so

void func(int i)

takes an int but returns nothing. In the case of parameters this:

void func()

is equivalent to this:

void func(void)

which indicates more explicitly that it does not take parameters. Different story is with the type void * which is a type, a pointer to something dimensionless.

Solution 4

Void means "emptyness". In your example of void main() it means that the functions main() does not return a value. I feel obliged tell you that void main() should be avoided (no pun intended) at all costs, use int main() instead. int main() makes sure your program can return a value of type int to the OS on close. There are numerous other uses of void, check out this website if you want to read more about this.

Share:
36,211
Joseph Lee
Author by

Joseph Lee

I have worked at Samsung Electro-Mechanics for 7.5 years as a PCB/PKG Engineer. I am currently working for a start-up as a firmware engineer. I graduated at Berkeley with a Bachelor's Degree in applied math and physics. I also graduated from the University of California at Irvine with a Master's Degree in materials science and engineering and another Master's Degree in electrical engineering at the University of Southern California. I am going to ask a lot of dumb questions about computer programming since I am a newbie. I also play the violin and piano. I hope you can enjoy my violin performances on YouTube. I am currently married to my wife JungEun since May 19, 2012. I hope that people will answer kindly what I have asked in terms of my questions.

Updated on February 04, 2020

Comments

  • Joseph Lee
    Joseph Lee over 4 years

    Possible Duplicate:
    Why is the type of the main function in C and c++ left to the user to define?

    What is a void ? Anyone provide some examples, proper use of void ? And what is the difference when we write void main (void) or main() ?

  • anishsane
    anishsane over 11 years
    So void bar() is same as void bar(...)?
  • Joseph Lee
    Joseph Lee over 11 years
    I checked the website, but I have heard that C and C++ are vastly different. I want to learn more about voids in terms of C.
  • Omkant
    Omkant over 11 years
    @anishsane : No,that's called variable number of arguments and to implement this bar(...) at least one argument is required which is the first argument and then only it will take ... so correct way is bar(char *,...)
  • Joseph Lee
    Joseph Lee over 11 years
    ??? If void func(int i) takes an integer but returns nothing, then what is the use? It seems it does nothing.
  • Kevin
    Kevin over 11 years
    Excuse me, I didn't read the tag, sorry :)
  • Joseph Lee
    Joseph Lee over 11 years
    I read many of the comments and questions on voids in this website. I thought "void main(void)" does not return any value. One person says that it should be avoided at all costs.
  • Joseph Lee
    Joseph Lee over 11 years
    I am beginning to understand what a void is in your explanations 1 and 2. Thank you. However, I do not understand what is a pointer. What is dereferenced mean?
  • Joseph Lee
    Joseph Lee over 11 years
    No problem. Thanks anyway.
  • Omkant
    Omkant over 11 years
    @JosephLee : yes, It must be avoided , see the standard I have mentioned in my answer.main() should have only two types of definitions and The implementation declares no prototype for this function
  • anishsane
    anishsane over 11 years
    Plus, value returned by main is returned to the OS, as the exit status of process. Hence main MUST return a value.
  • Omkant
    Omkant over 11 years
    @anishsane : I was also typing the same , this return status will tell you the execution of program is done successfully or it got terminated in the middle, And we follow the convention of 0 return value means success and any non-zero means error. In Linux terminal you can check the exit status of the previous command by echo $? , which is nothing but the return value of previous program
  • Ravi
    Ravi over 11 years
    @JosephLee sorry for using pointer. But, i just wanna to show you, where and how can we use void. If you don't know about pointer, then leave that part. Just concentrate of this part, void means no type
  • lunadir
    lunadir over 11 years
    no, it's very common, for example for interacting with the hardware
  • Lundin
    Lundin over 11 years
    Or more likely, void func() alters private variables declared as static at file scope.
  • Lundin
    Lundin over 11 years
    "doesn't mean that it's a void type, it depends on the compiler", yes if it is a C90 compiler, it will be equivalent to int main(). If it is a standard C compiler, it will not compile.
  • Lundin
    Lundin over 11 years
    Implicit int was removed in C99, which you cite. So main() without a specified return type will not compile. Also, void main() is perfectly fine on free running systems, read this.
  • Lundin
    Lundin over 11 years
    @anishsane Nobody has mentioned whether this is hosted (OS) or not. Also see C11 5.1.2.2.3: reaching the } that terminates the main function returns a value of 0.. So main() is a special case that doesn't follow the logic of other functions.
  • Lundin
    Lundin over 11 years
    @Kevin void main() should be avoided on hosted systems (programs running on an OS), because it will not compile on such systems.
  • Joseph Lee
    Joseph Lee over 11 years
    There is no reason to apologize. I want to learn. If you can say anything further, it will be very appreciated. If not, I will ask better questions about it later.
  • Ravi
    Ravi over 11 years
    As you said, you are beginner, so better to start step by step. Pointer is little tough to understand, so better, clear your basic first. And, Learner are always welcome in StackOverFlow. :)
  • anishsane
    anishsane over 11 years
    @Lundin: Then how does one explain this: stackoverflow.com/questions/3727051 ? Perhaps, we & our compilers do not stick to all the C standards in our day-to-day coding.
  • Lundin
    Lundin over 11 years
    @anishsane Most likely because the compiler that was used followed the older version of the standard, C89. As a programmer, it is your duty to pick a compiler that follows the standard you intend to write the program in. If you write code according to C99 but compile on a C89 compiler, then that is a bug caused by the programmer, not the compiler.
  • JianyuZhan
    JianyuZhan over 11 years
    @ anishsane. main needs a return type of int is mandatory in Hosted environment, but not in Freestanding environment.Freestanding is something without an OS.