Home > Backend Development > C++ > How to Choose Between `__func__`, `__FUNCTION__`, and `__PRETTY_FUNCTION__` for Accessing Function Names in C and C ?

How to Choose Between `__func__`, `__FUNCTION__`, and `__PRETTY_FUNCTION__` for Accessing Function Names in C and C ?

Susan Sarandon
Release: 2024-12-13 15:35:11
Original
861 people have browsed it

How to Choose Between `__func__`, `__FUNCTION__`, and `__PRETTY_FUNCTION__` for Accessing Function Names in C and C  ?

Function Name Macros in C and C

In C and C , several macros are available to access the name of the current function. These macros include __func__, __FUNCTION__, and __PRETTY_FUNCTION__.

func

Defined in C99 and adopted into C 11, func is an implicitly declared character array variable that holds the name of the enclosing function. It's a simple and lightweight option that provides the unadorned function name.

FUNCTION

FUNCTION is a pre-standard extension supported by certain compilers. Its behavior is similar to func__, but it's not part of the official language standards. It's preferable to use __func where available for portability reasons.

PRETTY_FUNCTION

PRETTY_FUNCTION is a GCC-specific extension that provides the "pretty" name of the function, including its signature. Unlike its predecessors, it's primarily useful in C to obtain the full function prototype with argument types.

Documentation

func is documented in the C99 standard, section 6.4.2.2/1. FUNCTION is not officially documented but is typically included in compiler-specific documentation. PRETTY_FUNCTION is documented on the GCC documentation page "Function Names as Strings."

When to Use Each Macro

The choice of macro depends on specific requirements:

  • Simple function name: func or FUNCTION is sufficient.
  • Complex function name with signature (C only): PRETTY_FUNCTION
  • Pre-C99 or non-C 11 compatibility: FUNCTION (if supported by the compiler)
  • Maximum portability: func

The above is the detailed content of How to Choose Between `__func__`, `__FUNCTION__`, and `__PRETTY_FUNCTION__` for Accessing Function Names in C and C ?. 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