+ 1

Can the main function in C++ be of type void?

yesterday i tried to declare the main() to be of type void but i encountered an error.

13th Jul 2016, 9:27 AM
chaitanya guruprasad
chaitanya guruprasad - avatar
7 Answers
+ 1
if u talk about c++ then yes in older complilers you can put a void return type for main for example you can use it in older versions of Borland turbo and in 4.5 version also but new compilers like Dev prohibits such a return type for main function if u insert int main (){//} and not return anything or zero it will do perfectly without any warnings and errors means no use of giving return type in Dev
16th Jul 2016, 8:40 AM
Afzaal Arif
Afzaal Arif - avatar
me too
13th Jul 2016, 9:43 AM
real kurama
real kurama - avatar
yes only if u r not returning a value.
13th Jul 2016, 11:16 AM
Yes you can but it doesn't return a value
13th Jul 2016, 5:58 PM
Krishna kittu
Krishna kittu - avatar
#include <iostream> using namespace std; main() { int a; cout << "Please enter a number \n"; cin >> a; cout << a; }
16th Jul 2016, 3:40 AM
In dev it will show error if u'll write void with main while if u compile it in turbo it will accept it.. In dev don't write anything before main like i have given one example of a program..
16th Jul 2016, 3:42 AM
yes it can be of type void when you want to just print a message or something which does not require to write in that case u can use void as return type
8th Aug 2016, 9:05 PM
sachin tomar
sachin tomar - avatar