首頁  >  文章  >  後端開發  >  在C語言中,預先定義標識符__func__

在C語言中,預先定義標識符__func__

王林
王林轉載
2023-08-30 12:49:06745瀏覽

在C語言中,預先定義標識符__func__

標識符是在程式設計中給予實體的名稱,以便在程式中進行識別。

通常,標識符是由程式設計師創建的,以實現高效工作,但也有一些預先定義的標識符內建在程式設計中。例如,cout、cin等。

在這裡,我們將看到C程式語言中的一個預先定義標識符__func__。

__func__的正式定義為 −

「標識符__func__應被翻譯器隱式聲明,就好像在每個函數定義的左花括號之後立即跟著宣告一樣。」

static const char __func__[] = “function-name”;

appeared, where function-name is the name of the lexically-enclosing function.」

C program The __func__ is a compiler-generated identifier that is created to identify the function using function name.

Let's see a few code examples to make the concept more clear,

#Example

 Live Demo

#include <stdio.h>
void function1 (void){
   printf ("%s</p><p>", __func__);
}
void function2 (void){
   printf ("%s</p><p>", __func__);
   function1 ();
}
int main (){
   function2 ();
   return 0;
}

Example

 Live Demo

function2
function1

Explanation

− 在這裡,我們使用了__func__方法來傳回被呼叫的函數的名稱。標識符傳回它被呼叫的函數的名稱。兩個print語句都呼叫了__func__來取得自己的方法引用。

這個標識符甚至可以在主方法中使用。例如,

範例

 線上示範

#include <stdio.h>
int main (){
   printf ("%s</p><p>", __func__);
   return 0;
}

輸出

main

But this cannot be overwritten i.e. __func__ is reserved for function names only. Using it to store anything else will return error.

Let's see

Let's see

Let's see

## Live Demo

#include <stdio.h>
int __func__ = 123;
int main (){
   printf ("%s</p><p>", __func__);
   return 0;
}

輸出

error
還有其他類似的功能在C程式語言中也可以進行類似的辨識工作。其中一些是

#__File__ - 傳回目前檔案的名稱。

__LINE__

- 傳回目前行的編號。

讓我們看一個程式碼來顯示實作

範例

 線上示範

#include <stdio.h>
void function1(){
   printf("The function: %s is in line: %d of the file :%s</p><p>", __func__,__LINE__,__FILE__);
}
int main(){
   function1();
   return 0;
}
輸出

The function: function1 is in line: 3 of the file :main.c
######Explanation### − 這些是一些通用函數,可能在我們收集有關檔案名、程式碼行和目前所呼叫的函數的資訊時會有用,使用__func__,__LINE__,__FILE__標識符。 ###

以上是在C語言中,預先定義標識符__func__的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除