cfunctionundefinedundeclared-identifier

First use in this function


I started learning C recently, and whenever I try to run this it's giving an error of "a" and "b" undeclared as first use in this function, even, though the course I was watching ran the same code with no erros, he just changed the number to 10 and 15 thats all

#define sum (a,b)(printf("%i", a + b));
int main ()
{

the enitre code is 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Name "JSDJSDJ"
#define Age 3000
#define sum (a,b) (printf("%i",a + b));

int main ()
{
    sum(100,150);
    printf ("%i", Age);

    return 0;
}
    sum (100,150);
}

Not much other than changing the numbers


Solution

  • When you define a macro with parameters, you can't have spaces between the macro name and (. This is an exception to the usual rule that whitespace is insignificant between tokens in C, because the preprocessor needs to be able to distinguish between a function-like macro and a macro that expands into something surrounded by ().

    So it should be

    #define sum(a,b) (printf("%i", (a) + (b)));
    

    You should also always wrap macro parameters in parentheses in the replacement list, to avoid problems with operator precedence when the argument is an expression.