简体   繁体   中英

"Hello World" not compiling due to "return(0)"?

simple question:

why does my code compile when it looks like this:

#include <unistd.h>
#include <stdio.h>

void hello()
{
    write (1, "3", 3);
    
}

int main ()
{
    hello();
}

Output: 3

And why does it not compile when I add return(0); in the line after write (1, "3", 3); ?

The error message I get is:

2function.c:7:2: error: void function 'hello' should not return a value [-Wreturn-type]
        return(0);

Thanks!

A function with a return type of void means that it returns no value. So if such a function uses a return statement with a value, that violates the definition of the function.

It's also not advisable to fail to return a value from a function with a return type other than void , even though compilers will allow it. The main function however is an exception. If no value is returned from main , the return value is assumed to be 0.

void means nothing. When your function return type is void, you shouldn't return anything. Or you can write only return; .

This is because your function

void hello()

is defined as type "void" - that is, you're defining it as something that does not return anything. You can add

return;

to the end, but only because it's not returning a value. if you would like to return 0 in function "hello" you would need to declare the data type you would like to return, instead of void, ie.

int hello()

that will then allow you to return any int, such as return 0;

You declared hello to return "void", which is to say nothing at all, yet you are trying to return 0.

For starters this call

write (1, "3", 3);

invokes undefined behavior at least because the string literal "3" contains only 2 characters (not 3 as you think) that is it is stored as a character array as { '3', '\0' } .

You declared the function hello as having the return type void . Such a function shall not return a value.

From the C Standard (6.8.6.4 The return statement)

1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

So the compiler issues an error relative to your inserted statement

return(0);

Instead you could just write

return;

though it does not make a great sense for such a function containing only one statement.

It seems you wanted to insert the statement

return(0);

in main like

int main( void )
{
    hello();

    return 0;    
}

though in main it may be omitted.

void means return nothing when you declare the function as void you cant return anything. but if you want to return something change the return type to int,float etc

To return anything from function you must specify the return type before function name. void means nothing and you are trying to return an integer from hello(). That's why you are getting error.

#include <unistd.h>
#include <stdio.h
int hello(){
   write (1, "3", 3);
   return 0; 
}

int main (){
    hello();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM