Home >Backend Development >C++ >What's the Difference Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?

What's the Difference Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-24 00:48:09344browse

What's the Difference Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?

Understanding the Differences: PRETTY_FUNCTION__, __FUNCTION__, and __func

In programming, accessing information about the current function can be useful for debugging, logging, and other purposes. Three commonly used identifiers in C and C serve this function: __PRETTY_FUNCTION__, __FUNCTION__, and __func__. Each one holds its own unique characteristics worth understanding.

__func__: A Standard Identifier from C99

Defined in C99, func is an implicitly declared identifier that expands to a character array containing the name of the function in which it is used. As a declared identifier, func behaves differently than a preprocessor macro.

__FUNCTION__: A Pre-Standard Extension

Widely supported by C compilers like gcc and Visual C , FUNCTION is not strictly a part of the C standard. It typically behaves similarly to __func__, but some inconsistencies between compilers may exist.

__PRETTY_FUNCTION__: A GCC Extension

PRETTY_FUNCTION is a gcc-specific extension that is comparable to __FUNCTION__. However, for C functions, it returns the "pretty" name, which includes the function's signature.

Choosing the Right Identifier

Selecting which identifier to use primarily depends on support and functionality requirements:

  • If standard C99 compliance is necessary, use __func__.
  • If you require a more portable option with behavior similar to __func__, consider using __FUNCTION__.
  • If your code is specifically designed for gcc and you want the "pretty" function name, employ __PRETTY_FUNCTION__.

It's also important to consult specific compiler documentation for nonstandard extensions like FUNCTION and __PRETTY_FUNCTION__. For example, Visual C offers similar extensions like __FUNCSIG__.

The above is the detailed content of What's the Difference Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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