Skip to content

Instantly share code, notes, and snippets.

@elieux
Created March 13, 2015 15:00
Show Gist options
  • Save elieux/9c7fc88248fd91a95ece to your computer and use it in GitHub Desktop.
Save elieux/9c7fc88248fd91a95ece to your computer and use it in GitHub Desktop.
GCC nested varargs error
#include <stdio.h>
#include <stdarg.h>
int sum(int count, ...) {
int result = 0;
va_list args;
va_start(args, count);
while (count--) {
result += va_arg(args, int);
}
va_end(args);
return result;
}
#define SUM(C, ...) sum(C, ##__VA_ARGS__)
int main() {
printf("Hello %d!", SUM(2, 13, SUM(2, 14, 15)));
return 0;
}
> gcc -c -std=c11 bad.c
bad.c: In function 'main':
bad.c:19:2: warning: implicit declaration of function 'SUM' [-Wimplicit-function-declaration]
printf("Hello %d!", SUM(2, 13, SUM(2, 14, 15)));
^
> g++ -c -xc++ -std=c++11 bad.c
bad.c: In function 'int main()':
bad.c:19:46: error: 'SUM' was not declared in this scope
printf("Hello %d!", SUM(2, 13, SUM(2, 14, 15)));
^
bad.c:16:30: note: in definition of macro 'SUM'
#define SUM(C, ...) sum(C, ##__VA_ARGS__)
^
> gcc -std=c11 good.c && good
Hello 42!
> cl /nologo good && good
Hello 42!
> cl /nologo bad && bad
Hello 42!
#include <stdio.h>
#define ADD(x, y) ((x) + (y))
int main() {
printf("Hello %d!", ADD(13, ADD(14, 15)));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment