What is the impact of using the void keyword before the main function in C?

In summary, putting void before the main function means that the function returns no value. This is valid syntax for most functions, but not for the main function. The main function must return an integer to be compliant with the standard, which indicates success or failure. Using void main() can lead to unpredictable return values and is not considered good practice. The C++ standard requires main to return int, while the C standard permits other return types but it is not considered portable.
  • #1
kthouz
193
0
What is the effect of putting void before main function i mean?

void main()
{
}
void main(void)
{
}
 
Technology news on Phys.org
  • #2
What you wrote means the function returns no value. This is valid syntax in general, but not for the main function. Many functions don't have a return value. For example, a function that receives a structure pointer as an argument and updates the contents of the structure might well have no return value.

The main function must return an integer (an int) to be compliant with the standard. The returned value indicates success (zero) or failure of some sort (non-zero).
 
  • #3
DH is completely right: void main() is not okay, 'not nowhow, not noway' as the Lion said to Dorothy.

If you leave it as void main(), when the program returns to the shell or whoever called it, the return value can be any integer, ie. whatever garbage is left in stack space. If it returns a zero, it is probably pure luck - programming by coincidence.
 
  • #4
main() should be defined as 'int main()' or in many cases 'int main(int argc, char * argv[])'.
 
  • #5
The C++ standard requires main to return int.

The C standard permits compilers to allow other return types -- but such code is, in principle, nonportable.
 

What is a function in C language?

A function in C language is a block of code that performs a specific task. It can be called multiple times from different parts of the program, making it a reusable component.

How do you declare a function in C language?

To declare a function in C language, you need to specify its return type, name, and parameters (if any). For example:
int sum(int num1, int num2);
This declares a function named "sum" that takes two integer parameters and returns an integer value.

What is the difference between call by value and call by reference in C language?

In call by value, the function receives a copy of the argument passed to it, while in call by reference, the function receives the address of the argument. This means that any changes made to the parameter inside the function will affect the original argument in call by reference, but not in call by value.

How do you return a value from a function in C language?

To return a value from a function in C language, you need to use the return statement. For example:
int sum(int num1, int num2) {
return num1 + num2;
}
This function will return the sum of the two parameters.

What is recursion in C language?

Recursion in C language is the process of a function calling itself. It is useful when solving problems that can be broken down into smaller, similar subproblems. However, it is important to have a base case that stops the recursive calls, otherwise the function will continue to call itself indefinitely.

Similar threads

  • Programming and Computer Science
Replies
6
Views
901
  • Programming and Computer Science
Replies
2
Views
680
  • Programming and Computer Science
Replies
4
Views
730
  • Programming and Computer Science
Replies
5
Views
806
  • Programming and Computer Science
Replies
2
Views
605
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
11
Views
999
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top