0
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define CHECK(x,y,n) ((x) > 0 && (x) < (n) && (y) > 0 && (y) < (n) ? 1 : 0)
#error(index) fprintf(stderr, "Range error: index = %d\n",index)
int main(void)
{
/*char s[5];
int i;
strcpy(s,"abcd");
i = 0;
putchar(s[++i]);*/
int i = CHECK(10,50,45);
    printf("%d",i);
  return 0;
}

I tries to do error input that takes an argument because macro can do that but when i try to build and compile i get this error message
#error (index) fprintf(stderr, "Range error: index = %d\n",index)
can i have an argument in the error directive or fprintf or whats is wrong?

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
jonathan
  • 5
  • 1
  • 5
  • 2
    You are using `#error` for something completely different from what it's supposed to do. See here: http://stackoverflow.com/questions/5323349/error-directive-in-c – Adrian Aug 22 '15 at 21:54
  • Sorry thanks i was confused – jonathan Aug 22 '15 at 22:07

1 Answers1

1

I try to do error input that takes an argument because macro can do that

First, #error isn't a macro. It's a directive. So it gets processed by the preprocessor. Meaning, it only works at compile time. In other words, it can't be used at runtime, say, after an if statement. This also explains that you, at least logically, can't demand an argument because the preprocessor wouldn't know what to do with it.

Second, as it seems you're starting to learn the preprocessor, I'll tell you that all it does is simple substitution. In fact, you can see the final form of the source file as the compiler sees it with the -E option to gcc. For example:

#define BIGGER(x,y) (x > y ? x : y)

int main(void) {
    int num = BIGGER(3, 7); // 7
}

Now, all the compiler sees is this:

int main(void) {
    int num = (3 > 7 ? 3 : 7);
}

I hope that clarifies your confusion.

Community
  • 1
  • 1
Amr Ayman
  • 1,129
  • 1
  • 8
  • 24