Home > Backend Development > C++ > Which C/C Identifier Should I Use to Get a Function's Name at Runtime?

Which C/C Identifier Should I Use to Get a Function's Name at Runtime?

Patricia Arquette
Release: 2024-12-13 00:13:14
Original
775 people have browsed it

Which C/C   Identifier Should I Use to Get a Function's Name at Runtime?

Selecting the Right Identifier for Function Name Acquisition

C and C offer several identifiers for retrieving the name of the active function during runtime: __PRETTY_FUNCTION__, __FUNCTION__, and __func__. This guide explores the differences between these identifiers and provides guidance on their usage.

__func__:

"__func__" is a C99-introduced identifier that represents a character array containing the function's name. It is implicitly defined within each function. In C , func was introduced in C 11, offering an implementation-defined string.

__FUNCTION__:

"__FUNCTION__" is a pre-standard extension supported by compilers like gcc and Visual C . However, it is recommended to use func where supported and FUNCTION only when func is unavailable.

__PRETTY_FUNCTION__:

"__PRETTY_FUNCTION__" is a gcc-specific extension that resembles __FUNCTION__. For C functions, it displays the "pretty" name, including the signature. Visual C has a similar extension, __FUNCSIG__.

Deciding Which Identifier to Use:

The choice depends on compiler support and the desired function name format:

  • Use func when available (supported in most modern compilers)
  • Use FUNCTION if func is unavailable
  • Use PRETTY_FUNCTION or FUNCSIG for the "pretty" name with the function signature (gcc or Visual C only)

Documentation:

  • __func__: C99 §6.4.2.2/1
  • func (C ): C 11 §8.4.1[dcl.fct.def.general]/8
  • __FUNCTION__, __PRETTY_FUNCTION__: gcc documentation, "Function Names as Strings"
  • __FUNCSIG__: MSDN documentation, "Predefined Macros" (Visual C )

The above is the detailed content of Which C/C Identifier Should I Use to Get a Function's Name at Runtime?. 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