Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use #define to create function factory in C

1 Answer

0 votes
#include <stdio.h>
 
// create function factory
#define FUNCTION(name, n) int fun_##name(int x) { return n * x;}
 
FUNCTION(f1, 3)
FUNCTION(f2, 4)
 

int main(void)
{
    printf("f1(15): %d\n", fun_f1(15) );
    printf("f2(17): %d\n", fun_f2(17) );
    
    return 0;
}

 
/*
run:

f1(15): 45
f2(17): 68

*/

 



answered Aug 23, 2016 by avibootz

Related questions

1 answer 109 views
1 answer 136 views
1 answer 97 views
97 views asked Mar 7, 2024 by avibootz
2 answers 109 views
109 views asked Feb 9, 2023 by avibootz
1 answer 162 views
1 answer 158 views
1 answer 139 views
...