在C语言中如何在函数中打印变量名。

huangapple go评论53阅读模式
英文:

How to print variable name in function in C

问题

I want to print the name of a 2D matrix I take as input in a function.

I tried using this I found on the internet:

#include <stdio.h>
#define VariableName(var) #var

void saddle(int a[][100], int m, int n)
{
    printf("The saddle points of the matrix %s are: \n", VariableName(a));
}

But on every function call I get the name as a instead of the matrix I entered, eg. A, B, or C.
How to get past this?

英文:

I want to print the name of a 2D matrix I take as input in a function.

I tried using this I found on the internet:

#include &lt;stdio.h&gt; 
#define VariableName(var) #var   

void saddle(int a[][100], int m, int n)
{
    printf(&quot;The saddle points of the matrix %s are: \n&quot;, VariableName(a));
}
 `

But on every function call I get the name as a instead of the matrix I entered, eg. A, B, or C.
How to get past this?

答案1

得分: 4

VariableName(var)函数在您的代码中始终生成字符串&quot;a&quot;。矩阵的名称仅在调用处知道,因此您应该将其作为额外参数传递,并且您可以在那里使用相同的宏来创建字符串:

void saddle(int a[][100], int m, int n, const char *name)
{
    printf("矩阵 %s 的鞍点为:\n", name);
}

void print_matrix(int a[][100], int m, int n, const char *name)
{
    printf("矩阵 %s 的内容为:\n", name);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; i++)
            printf(" %3d", a[i][j]);
        printf("\n");
    }
}

int main(void)
{
    int matrix1[10][100] = { 0 };
    int matrix2[10][100] = { 0 };
    saddle(matrix1, 10, 10, VariableName(matrix1));
    saddle(matrix2, 10, 10, VariableName(matrix2));
    print_matrix(matrix1, 10, 10, VariableName(matrix1));
    print_matrix(matrix2, 10, 10, VariableName(matrix2));
    return 0;
}
英文:

VariableName(var) always produces the string &quot;a&quot; in your code. The name of the matrix is only known at the calling site, so you should pass it as an extra argument and you can use the same macro there to create the string:

#include &lt;stdio.h&gt; 

#define VariableName(var) #var   

void saddle(int a[][100], int m, int n, const char *name)
{
    printf(&quot;The saddle points of the matrix %s are:\n&quot;, name);
}

int main(void)
{
    int matrix[100][100];
    saddle(matrix, 100, 100, VariableName(matrix));
    return 0;
}

Using &quot;matrix&quot; instead of VariableName(matrix) seems simpler, but here is a more advanced version where the matrix name or expression is passed implicitly:

#include &lt;stdio.h&gt; 

#define VariableName(var) #var   

#define saddle(a,m,n)  (saddle)(a, m, n, VariableName(a))
#define print_matrix(a,m,n)  (print_matrix)(a, m, n, VariableName(a))

void (saddle)(int a[][100], int m, int n, const char *name)
{
    printf(&quot;The saddle points of the matrix %s are:\n&quot;, name);
}

void (print_matrix)(int a[][100], int m, int n, const char *name)
{
    printf(&quot;Contents of matrix %s:\n&quot;, name);
    for (int i = 0; i &lt; m; i++) {
        for (int j = 0; j &lt; n; i++)
            printf(&quot; %3d&quot;, a[i][j]);
        printf(&quot;\n&quot;);
    }
}

int main(void)
{
    int matrix1[10][100] = { 0 };
    int matrix2[10][100] = { 0 };
    saddle(matrix1, 10, 10);
    saddle(matrix2, 10, 10);
    print_matrix(matrix1, 10, 10);
    print_matrix(matrix2, 10, 10);
    return 0;
}

huangapple
  • 本文由 发表于 2023年5月15日 13:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定