How can we know the caller function's name?

后端 未结 10 1640
北海茫月
北海茫月 2020-12-02 10:26

In the C language, __FUNCTION__ can be used to get the current function\'s name. But if I define a function named a() and it is called

10条回答
  •  孤街浪徒
    2020-12-02 11:14

    You can tag each function that calls a() with an integer identifier which is passed to a() as a parameter and then use a switch-case construct in a() to tell which function has invoked a().A printf() would tell which function invoked a() depending on the integer identifier value if you use that as an argument to a switch-case construct in a()

    #include
    
    void a(int);
    void b();
    void c();
    void d();
    
    int main(void)
    {
    
    b();
    c();
    d();
    
    }
    
    void b()
    {
    
    int x=1;
    a(x);
    
    }
    
    void c()
    {
    
    int x=2;
    a(x);
    
    }
    
    void d()
    {
    
    int x=3;
    a(x);
    
    }
    
    void a(int x)
    {
    
    switch(x)
    {
    case 1:
    printf("b called me\n");
    break;
     case 2:
    printf("c called me\n");
    break;
    case 3:
    printf("d called me\n");
    }
    
    }
    

提交回复
热议问题