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,885 questions

51,811 answers

573 users

How to use generic print function with macro in C

1 Answer

0 votes
#include <stdio.h>

void print_int(int i) { printf("int: %d\n", i); }
void print_double(double d) { printf("double: %g\n", d); }
void print_string(char *s) { printf("string: %s\n", s); }
void print_default() { puts("unknown argument"); }

#define print(X) _Generic((X), \
            int: print_int, \
            double: print_double, \
            char *: print_string, \
            default: print_default)(X)

int main(void) 
{
    print(100);
    print(3.14);
    print("c c++");
    print(10000000000000000L);
    
    return 0;
}
    
/*
run:
 
int: 100
double: 3.14
string: c c++
unknown argument

*/

 



answered Aug 21, 2017 by avibootz

Related questions

1 answer 118 views
1 answer 138 views
2 answers 166 views
166 views asked Jul 29, 2017 by avibootz
1 answer 140 views
140 views asked Jul 29, 2017 by avibootz
1 answer 175 views
2 answers 203 views
...