Home > Backend Development > C++ > Why Doesn't My Non-Void C Function Returning No Value Produce a Compiler Error?

Why Doesn't My Non-Void C Function Returning No Value Produce a Compiler Error?

Susan Sarandon
Release: 2024-12-16 08:27:14
Original
958 people have browsed it

Why Doesn't My Non-Void C   Function Returning No Value Produce a Compiler Error?

Non-Void Function Does Not Return a Value in C Code

Question:

This C code below has a non-void function that does not return a value:

static tvec4 Min(const tvec4& a, const tvec4& b, tvec4& out)
{
    tvec3::Min(a,b,out);
    out.w = min(a.w,b.w);
}
Copy after login

Why does it compile without an error?

Answer:

This behavior is undefined according to the C 11 draft standard section 6.6.3 paragraph 2, which states that flowing off the end of a value-returning function results in undefined behavior.

Compiler Warnings and Errors:

  • gcc and clang: Can generate a warning using the -Wall flag (warning: control reaches end of non-void function [-Wreturn-type]). This can be converted to an error using the -Werror=return-type flag.
  • Visual Studio: Generates error C4716 ('Min' : must return a value) by default, or warning C4715 if not all code paths return a value.

Implications:

Undefined behavior can lead to unpredictable results, including program crashes. It is recommended to use compiler options such as -Wall and -Wextra to detect and fix instances of undefined behavior.

The above is the detailed content of Why Doesn't My Non-Void C Function Returning No Value Produce a Compiler Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template