Skip to content

Instantly share code, notes, and snippets.

@barosl
Created July 26, 2015 07:26
Show Gist options
  • Select an option

  • Save barosl/e0af4a92b2b8cabd05a7 to your computer and use it in GitHub Desktop.

Select an option

Save barosl/e0af4a92b2b8cabd05a7 to your computer and use it in GitHub Desktop.

Revisions

  1. barosl created this gist Jul 26, 2015.
    26 changes: 26 additions & 0 deletions add.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int addi(int a, int b) {
    return a + b;
    }

    char *adds(char *a, char *b) {
    char *res = malloc(strlen(a) + strlen(b) + 1);
    strcpy(res, a);
    strcat(res, b);
    return res;
    }

    #define add(a, b) _Generic(a, int: addi, char*: adds)(a, b)

    int main(void) {
    int a = 1, b = 2;
    printf("%d\n", add(a, b)); // 3

    char *c = "hello ", *d = "world";
    printf("%s\n", add(c, d)); // hello world

    return 0;
    }