简体   繁体   English

“Hello World”由于“return(0)”而未编译?

[英]"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 Output:3

And why does it not compile when I add return(0);为什么当我添加return(0);时它不编译? in the line after write (1, "3", 3);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.返回类型为void的 function 表示它不返回任何值。 So if such a function uses a return statement with a value, that violates the definition of the function.因此,如果这样的 function 使用带值的return语句,则违反了 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.从 function 返回一个返回类型不是void的值也是不可取的,即使编译器允许它。 The main function however is an exception.然而main的 function 是个例外。 If no value is returned from main , the return value is assumed to be 0.如果main没有返回值,则假定返回值为 0。

void means nothing. void没有任何意义。 When your function return type is void, you shouldn't return anything.当您的 function 返回类型为 void 时,您不应返回任何内容。 Or you can write only return;或者你可以只写return; . .

This is because your function这是因为你的 function

void hello()

is defined as type "void" - that is, you're defining it as something that does not return anything.被定义为“void”类型——也就是说,您将其定义为不返回任何内容的东西。 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.如果您想在 function“hello”中返回 0,则需要声明要返回的数据类型,而不是 void,即。

int hello()

that will then allow you to return any int, such as return 0;然后将允许您返回任何 int,例如 return 0;

You declared hello to return "void", which is to say nothing at all, yet you are trying to return 0.您声明 hello 以返回“void”,也就是说什么也没有,但您却试图返回 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' } .调用未定义的行为至少是因为字符串文字"3"仅包含 2 个字符(不是您认为的3 ),它被存储为字符数组{ '3', '\0' }

You declared the function hello as having the return type void .您将 function hello声明为具有返回类型void Such a function shall not return a value.这样的 function 应该没有返回值。

From the C Standard (6.8.6.4 The return statement)来自 C 标准(6.8.6.4 返回语句)

1 A return statement with an expression shall not appear in a function whose return type is void. 1带有表达式的 return 语句不应出现在返回类型为 void 的 function 中。 A return statement without an expression shall only appear in a function whose return type is void.没有表达式的 return 语句只能出现在返回类型为 void 的 function 中。

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.尽管对于这样一个仅包含一个语句的 function 来说意义不大。

It seems you wanted to insert the statement看来你想插入语句

return(0);

in main like main喜欢

int main( void )
{
    hello();

    return 0;    
}

though in main it may be omitted.尽管在main中它可以被省略。

void means return nothing when you declare the function as void you cant return anything. void 表示当您将 function 声明为 void 时,您什么都不返回,您不能返回任何东西。 but if you want to return something change the return type to int,float etc但是如果你想返回一些东西,将返回类型更改为int,float

To return anything from function you must specify the return type before function name.要从 function 返回任何内容,您必须在 function 名称之前指定返回类型。 void means nothing and you are trying to return an integer from hello(). void 没有任何意义,您正试图从 hello() 返回 integer。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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