C语言 警告:表达式结果未使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19261171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:40:22  来源:igfitidea点击:

warning: expression result unused

c

提问by user2840926

char change(const char c){
  (c >= 'A')&&(c <= 'M') ? (c+'N'-'A') : 
((c >= 'N')&&(c <= 'Z') ? (c-('N'-'A')) : 
((c >='a')&&(c <= 'm') ? (c+'n'-'a') :
((c >= 'n')&&(c <= 'z') ? (c-('n'-'a')) : c )));
}

Why I get "warning: expression result unused" and "error: control reaches end of non-void function [-Werror,-Wreturn-type]"?

为什么我会收到“警告:未使用的表达式结果”和“错误:控制到达非空函数 [-Werror,-Wreturn-type] 的末尾”?

回答by dasblinkenlight

You get the warning because the expression gets calculated, and then the result is dropped. This is related to the "reaching the end of the function without returning a value" error: adding returnin front of the expression will fix both:

您收到警告是因为计算了表达式,然后删除了结果。这与“到达函数末尾而不返回值”错误有关:return在表达式前面添加将同时修复:

char change(const char c) {
    return (c >= 'A') && (c <= 'M') ? 
        (c+'N'-'A') :  ((c >= 'N') && (c <= 'Z') ? 
             (c-('N'-'A')) : ((c >='a') && (c <= 'm') ? 
                 (c+'n'-'a') : ((c >= 'n') && (c <= 'z') ? 
                     (c-('n'-'a')) : c )));
}