First use in this function

85 Views Asked by At

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

2

There are 2 best solutions below

7
Barmar On BEST ANSWER

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.

1
pm100 On

If you really want a 'function' then the code should look like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Name "JSDJSDJ"
#define Age 3000

void sum (int a, int b){
 printf("%i",a + b));
}

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

    return 0;
 }