How to achieve function overloading in C?

前端 未结 14 2315
清歌不尽
清歌不尽 2020-11-22 03:16

Is there any way to achieve function overloading in C? I am looking at simple functions to be overloaded like

foo (int a)  
foo (char b)  
foo (float c , i         


        
14条回答
  •  误落风尘
    2020-11-22 03:57

    I hope the below code will help you to understand function overloading

    #include 
    #include
    
    int fun(int a, ...);
    int main(int argc, char *argv[]){
       fun(1,10);
       fun(2,"cquestionbank");
       return 0;
    }
    int fun(int a, ...){
      va_list vl;
      va_start(vl,a);
    
      if(a==1)
          printf("%d",va_arg(vl,int));
       else
          printf("\n%s",va_arg(vl,char *));
    }
    

提交回复
热议问题