Click here to Skip to main content
15,029,128 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is this possible to return void, float, double, char, pointer type data from main() function?

C++
#include<stdio.h>
double main(){
printf("No may be not!!");
return 0.0;
}

C++
#include<stdio.h>
float main(){
printf("No may be not!!");
return 0.0;
}


C++
#include<stdio.h>
 *main(){
printf("No may be not!!");
int n=9;
int *a;
a=n;
return (&a);
}


C++
#include<stdio.h>
char main(){
printf("No may be not!!");
return '\0';
}


What I have tried:

I want them compiled in codeblock though I got error message. Why? Can't main return anything as above?
Posted
Updated 13-Mar-16 23:58pm
v2

main() is a special function and by convention it returns an integer value to the host OS.
   
Comments
Afzaal Ahmad Zeeshan 13-Mar-16 15:05pm
   
5ed; — "...host OS or the host framework" should suffice.
Mehdi Gholam 14-Mar-16 1:15am
   
Cheers Afzaal!
Because main have to return an int.
See ISO/IEC 9899 (C99) and ISO/IEC 9899:201x (C11) at §5.1.2.2.1 Program startup:
Quote:

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.

You can define main with the 2 parameters int argc and char *argv[], or nothing. But the returns, as per all current standards, cannot be void.
Anyway some compilers allow the form void main(void) { /* ... */ } as private extension not standard compliant.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900