{"id":2572,"date":"2024-05-10T07:23:58","date_gmt":"2024-05-10T07:23:58","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2572"},"modified":"2024-05-10T07:23:58","modified_gmt":"2024-05-10T07:23:58","slug":"predefined-identifier-func-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/predefined-identifier-func-in-c\/","title":{"rendered":"Predefined Identifier __Func__ In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#predefined-identifier-func-in-c\">Predefined Identifier __func__ In C<\/a><\/li><li ><a href=\"#to-illustrate-the-working-of-predefined-identifier-func\">To illustrate the working of predefined identifier __func__<\/a><\/li><li ><a href=\"#example-1-to-illustrate-func\">Example 1: To illustrate _func_<\/a><\/li><li ><a href=\"#example-2-to-demonstrate-the-double\">Example 2: To demonstrate the double <\/a><\/li><li ><a href=\"#faq-predefined-identifier-func-in-c\">FAQ-Predefined Identifier __func__ In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"predefined-identifier-func-in-c\">Predefined Identifier __func__ In C<\/h2>\n\n\n\n<p>In C programming, there&#8217;s a handy tool called <code>__func__<\/code>. It automatically tells you the name of the function you&#8217;re in. This makes it easier to find and fix problems in your code. This article explores how <code>__func__<\/code> works and why it&#8217;s helpful for programmers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"to-illustrate-the-working-of-predefined-identifier-func\">To illustrate the working of predefined identifier __func__<\/h2>\n\n\n\n<p>Before we begin discussing&nbsp;<em>__func__<\/em>, We can write some code snippets and anticipate the output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate working of a\n\/\/ Predefined Identifier __func__\n \n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ %s indicates that the program will read strings\n    printf(\"%s\", __func__);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main\n<\/code><\/pre>\n\n\n\n<p>The C language standard, including versions like C99 and C11, defines a predefined identifier as explained in clause 6.4.2.2.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u201cThe identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration\n\nstatic const char __func__&#91;] = \"function-name\";\nappeared, where function-name is the name of the lexically-enclosing function.\u201d<\/code><\/pre>\n\n\n\n<p> In C99 and later versions (including C11), the <code>__func__<\/code> identifier is predefined by the C compiler and automatically added to every function. It provides a way for the programmer to access the name of the current function within that function. This can be useful for debugging and error reporting purposes, allowing you to log or print the function name without explicitly specifying it in your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-to-illustrate-func\">Example 1: To illustrate _func_<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to demonstrate __func__\n#include &lt;stdio.h&gt;\n \n\/\/ %s is used to read strings\nvoid foo(void) { printf(\"%s\", __func__); }\nvoid bar(void) { printf(\"%s\", __func__); }\n \nint main()\n{\n    foo();\n    bar();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foobar\n<\/code><\/pre>\n\n\n\n<p>Predefined identifiers like <code>__func__<\/code> are very useful for logging and debugging purposes, especially in larger programs. They provide a convenient way to access the name of the current function without having to manually specify it, which can be error-prone and tedious, particularly in extensive codebases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-to-demonstrate-the-double\">Example 2: To demonstrate the double <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program to demonstrate double\n\/\/ predefined identifier or __func__\n#include &lt;stdio.h&gt;\n \nint __func__ = 10;\nint main()\n{\n    printf(\"%s\", __func__);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>In C, it&#8217;s essential to adhere to the C standard&#8217;s specifications, which explicitly state that predefined identifiers like <code>__func__<\/code> are automatically defined by the compiler for each function with their respective function names. Attempting to explicitly define or redefine these predefined identifiers results in undefined behavior, and it&#8217;s best to avoid such practices.<\/p>\n\n\n\n<p>Additionally, you rightly pointed out the significance of other predefined macros like <code>__FILE__<\/code> and <code>__LINE__<\/code>. These macros, as mentioned in the C standard clause 6.10.8, provide valuable context information about the source file and line number, which is incredibly useful for debugging, error reporting, and tracking code execution. Following the C standard and using these predefined macros as intended is crucial for writing robust and reliable C programs.<\/p>\n\n\n\n<p><strong>Output of the following code snippet<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate __FILE__, func, line\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    printf(\"In file:%s, function:%s() and line:%d\",\n           __FILE__, __func__, __LINE__);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-predefined-identifier-func-in-c\">FAQ-Predefined Identifier __func__ In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696853020342\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What is the __ func __ identifier in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In a C program, <code>__func__<\/code> is a special identifier created by the compiler. It&#8217;s automatically set to the name of the function where it&#8217;s used, helping you identify which function is currently being executed.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696853041237\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is a predefined function in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Library functions in C, also known as predefined functions, are functions that are already defined in the C libraries. This means you don&#8217;t have to write the function&#8217;s code yourself; you can simply call these functions to perform specific tasks. They are ready-made tools you can use to make your C programming tasks easier and more efficient.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696853048076\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are predefined macros in C language?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>__OBJC__<\/code> macro is like a switch that helps you determine if the Objective-C compiler is being used. It has a value of 1 when Objective-C is in use, allowing you to conditionally include or exclude code based on whether you&#8217;re compiling with Objective-C or not.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Predefined Identifier __func__ In C In C programming, there&#8217;s a handy tool called __func__. It automatically tells you the name of the function you&#8217;re in. This makes it easier to find and fix problems in your code. This article explores how __func__ works and why it&#8217;s helpful for programmers. To illustrate the working of predefined &#8230; <a title=\"Predefined Identifier __Func__ In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/predefined-identifier-func-in-c\/\" aria-label=\"More on Predefined Identifier __Func__ In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2573,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[433],"class_list":["post-2572","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-predefined-identifier-__func__-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2572"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2572\/revisions"}],"predecessor-version":[{"id":10658,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2572\/revisions\/10658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2573"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}