Click here to Skip to main content
15,888,106 members
Please Sign up or sign in to vote.
2.56/5 (5 votes)
See more:
Hi expert, My c code is here:

C++
#include<stdio.h>
void main(){

printf("Hello world");


}


The codeblock shows the error:

||warning: command line option '-Wzero-as-null-pointer-constant' is valid for C++/ObjC++ but not for C [enabled by default]|
C:\Users\Amir Khasru\Desktop\pointers.c|2|error: return type of 'main' is not 'int' [-Wmain]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


What I have tried:

I tried to compile the above code through codeblock though never succeed. Please explain this?
Posted
Updated 19-Jan-22 21:39pm
v10
Comments
PIEBALDconsult 12-Mar-16 13:57pm    
What compiler? What error do you get?
Please use Improve question to add context and detail.
Member 12378355we 12-Mar-16 14:11pm    
Code block!! I updated!
PIEBALDconsult 12-Mar-16 14:23pm    
OK, so it looks like it's a non-conforming compiler, how odd. Change the return type to int or use a better compiler.
Member 12378355we 12-Mar-16 15:24pm    
Thanks
Member 12378355we 12-Mar-16 15:40pm    
So we could even return char,float,double, pointer type data from main()....:))

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;10) 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.
 
Share this answer
 
From a C language point of view, this code is perfectly legal.
C++
#include<stdio.h>
void main(){
    printf("Hello world");
}

So the only possibility is that the compiler do not conform to C language or do not run in C mode. As PIEBALDconsult said, "It is compiler dependent".
 
Share this answer
 
v3
Comments
Member 12378355we 13-Mar-16 6:55am    
Could char/float/double/* main() be possible?
PIEBALDconsult 13-Mar-16 13:22pm    
No.
manoranjan 14-Mar-16 3:37am    
This is not the right answer. main() function returns int (and not void). See Solution 2.

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