I simply don't get! #define
#1

This simple code:

Код:
#include <a_samp>

#if defined printf
  #undef printf
#endif

main(){

}
Doesn't compile! Why!?


It seems like this line
Код:
#if defined printf
finds printf to be defined and tries to execute this line
Код:
#undef printf
but compiler emits error on this as it doesn't recognize printf as defined anymore... wtf? Explain me please
Reply
#2

You can't simply "undefine" just anything. The manual says:
Quote:

#undef name
Removes a text substitution macro or a numeric constant declared
with const. The “name” parameter must be the macro “prefix”
—the alphanumeric part of the macro. See page 93 for details and
examples on text substitution.

Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
You can't simply "undefine" just anything. The manual says:
Ok, thanks; I got it. Here is one more:

Код:
#include <a_samp>

#define _FIRST print

main(){

  _FIRSTf("%s", "Test string");

}
Here is my understanting:
Pre-compiler meets #define. From now on it searches for any instance of "_FIRST" and replaces it with "print". Just like a human being would do it using text replacement tool.

But:
It doesn't work this exact way and throws an error about "_FIRSTf" being not defined. How does it work then? Sometimes it replaces chunks of code, sometimes it doesn't.
Reply
#4

- CUT -

The predecessor searches only for whole words.

To do _FIRSTf
You need to #define _FIRSTf printf
Reply
#5

Quote:
Originally Posted by Crayder
Посмотреть сообщение
This line, #undef printf, undefined the printf. So it will not be defined, it's pretty self explanatory.
It doesn't. Compiler throws an error, that such a constant "printf" doesn't exist and thus can't be undefined.
Reply
#6

Quote:
Originally Posted by Baltazar
Посмотреть сообщение
It doesn't. Compiler throws an error, that such a constant "printf" doesn't exist and thus can't be undefined.
printf is not a constant in the script. So it is correct. It's a native function.
Reply
#7

Quote:
Originally Posted by Crayder
Посмотреть сообщение
printf is not a constant in the script. So it is correct. It's a native function.
But well, this line
Код:
#if defined printf
finds "printf" to be defined although it's not a constant. Anyway, you are right and I kind of got it
Reply
#8

pawn Код:
#define _FIRSTf printf
Works fine for me.

Код:
doesn't exist and thus can't be undefined.
Those are two different things. They either exist and are defined, or do not exist and are undefined. You can't have both.
Reply
#9

Quote:
Originally Posted by Baltazar
Посмотреть сообщение
finds "printf" to be defined although it's not a constant. Anyway, you are right and I kind of got it
Aha, I don't think I'm right completely. I think I should take another look at the manual, you should as well.

http://www.compuphase.com/pawn/Pawn_Language_Guide.pdf
Reply
#10

Quote:
Originally Posted by Threshold
Посмотреть сообщение
Код:
doesn't exist and thus can't be undefined.
Those are two different things. They either exist and are defined, or do not exist and are undefined. You can't have both.
By saying "can't be undefined" I mean "you can't undefine it".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)