Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Contributor: Ravishankar ChavareWhat is difference between void main(),int main() in C Programs
#1
Hello Friends C programming language is called mother language of any programming language if you learn concepts in c that concepts help you to learn other languages easily.Today i am teaching you whats is the exact difference between
-void main()
-int main()
[Image: QgO70iJ.png]
Before getting the difference  between both function first learn how main() function work 

1)main()
 -Every programs in c start with main function.
 -after writing the c code we need to compile it (Compile means we can find some compile time error which are some syntax error missing parenthesis,Or our Typing mistakes called compile time error)
 -Note that our compiler don't show the run time error.
 -After Successfully completion of compiler without error we need to execute our c programs.
 -When we Run our c program operating system have control on it its will find out the main function in programs and start doing execution of our written code  


2)void main()

 -According to old ANSI C standard we are using void main().
 -If we don't need any return value we use return type 'void'
 -And Notice that when we using void main() we don't return any statement  in the main block to return the content to operating system

Syntax
Code:
void main(){

printf("Hello Protutr");

}


 -Here is the one problem that void doesn't return any value then our operating system do not get any acknowledge  to that our C program successfully completed or not so Operating a system confused that and you got such situation where when you running a c program then sometime its crashed.
 -and we are using here void so we doesn't have any way to say operating system that our programs crashed at this moment
 -So try to ignore using void main()


3)int main()

 -int main() function return a integer value after function completion that is zero(0) or Non-Zero Value.
 -You already notice that when you are using int main() then we use return 0; statement
Syntax:

Code:
int main(){

printf("Hello Protutr");

return 0;//successfully completion of running process
        //non zero value failed at some state
}

 -return 0 ->if our operating system reach at this stage then they got acknowledge that completion of the running programs
  return nonzero-> if operating system doesn't reach at the return 0; statement then non zero value shared to operating system means that programs got stuck at some place

 -So Use int main() instead of using void main(); this is suggested answer

Thanks for reading this tutorial if any suggestion comment it down
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)