10

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

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Joseph Lee
  • 529
  • 1
  • 5
  • 10
  • 1
    I suggest reading http://en.wikipedia.org/wiki/Void_type It contains useful information like this sentence: "The void type may also appear as the sole argument of a function prototype **to indicate that the function takes no arguments**." – Grüse Dec 14 '12 at 10:32
  • 1
    You may find your answer here: http://stackoverflow.com/q/3711048/1284631 (this question is a duplicate) – user1284631 Dec 14 '12 at 10:33
  • I don't see this as a duplicate at all. – Evan Carroll Feb 04 '19 at 01:32

5 Answers5

13

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.

nalzok
  • 14,965
  • 21
  • 72
  • 139
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • So `void bar()` is same as `void bar(...)`? – anishsane Dec 14 '12 at 10:47
  • @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 *,...)` – Omkant Dec 14 '12 at 10:56
  • 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 Dec 14 '12 at 11:12
  • @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` – Omkant Dec 14 '12 at 11:20
  • 1
    Plus, value returned by main is returned to the OS, as the exit status of process. Hence main MUST return a value. – anishsane Dec 14 '12 at 11:21
  • @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 – Omkant Dec 14 '12 at 11:24
  • 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](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593). – Lundin Dec 14 '12 at 12:42
  • 1
    @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 Dec 14 '12 at 12:44
  • @Lundin: Then how does one explain this: http://stackoverflow.com/questions/3727051 ? Perhaps, **we & our compilers do not stick to all the C** ***standards*** in our day-to-day coding. – anishsane Dec 14 '12 at 17:47
  • @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. – Lundin Dec 17 '12 at 09:50
  • @ anishsane. main needs a return type of int is mandatory in **Hosted environment**, but not in **Freestanding environment**.Freestanding is something without an OS. – l4rmbr Dec 19 '12 at 06:07
2

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.

lunadir
  • 339
  • 3
  • 15
  • ??? If void func(int i) takes an integer but returns nothing, then what is the use? It seems it does nothing. – Joseph Lee Dec 14 '12 at 10:58
  • no, it's very common, for example for interacting with the hardware – lunadir Dec 14 '12 at 12:03
  • Or more likely, void func() alters private variables declared as static at file scope. – Lundin Dec 14 '12 at 12:33
  • "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 Dec 14 '12 at 12:40
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

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • 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 Dec 14 '12 at 11:15
  • @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` – Ravi Dec 14 '12 at 11:55
  • 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. – Joseph Lee Dec 14 '12 at 13:11
  • 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. :) – Ravi Dec 14 '12 at 13:18
1

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.

Kevin
  • 2,739
  • 33
  • 57
  • 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. – Joseph Lee Dec 14 '12 at 10:49
  • Excuse me, I didn't read the tag, sorry :) – Kevin Dec 14 '12 at 11:09
  • @Kevin void main() should be avoided on hosted systems (programs running on an OS), because it will not compile on such systems. – Lundin Dec 14 '12 at 12:59
0

void is a data type with no values. It is also an incomplete data type that cannot be completed. When used as a return type for a function, it indicates that the function does not return a value:

void foo(int x);

When used as a parameter list, it indicates that the function takes no arguments:

void bar(void);

This is different from an empty parameter list, which indicates that the function takes an unspecified number of arguments (in C; in C++, an empty parameter list is the same as using void):

void bletch();

No object (variable) can be typed void. However, you can declare pointers of type void *: these are "generic" pointers, and can be converted to and from other pointer types without an explicit cast. The standard memory allocation functions malloc, calloc, and realloc all return void *:

double *darr = malloc(sizeof *darr * rows);

In a hosted implementation (basically, anything with an operating system), main must be declared as

int main(void)

or

int main(int argc, char **argv) // the parameter names can be whatever you want,
                                // but argc and argv are the usual convention;
                                // char **argv is equivalent to char *argv[]

or in some other implementation-defined manner; an implementation may accept

void main()

as a legitimate signature for main, but it must explicitly document that somewhere.

John Bode
  • 119,563
  • 19
  • 122
  • 198