Const vs Define
#1

After reading the Useful Snippets thread today, I got totally confused with the pros & cons of defines and constants.

So, I'm looking for an answer to this question: Which code below is better and why?

1.
pawn Код:
#define THIS "this or that"

main()
{
    new string[128], rand = random(100);

    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
    format(string, sizeof(string), "I like " THIS " a lot also I like the number %d.", rand);
    print(string);
}
2.
pawn Код:
static const THIS[] = "this or that";

main()
{
    new string[128], rand = random(100);

    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
    format(string, sizeof(string), "I like %s a lot also I like the number %d.", THIS, rand);
    print(string);
}
Edit: Silly me, fixed the examples.
Reply
#2

Definitions are processed by the preprocessor, constants are not. Though, I'm not exactly sure which one's better to use on lower levels (like new const myVar = 1). Of course you can't use definitions for larger declarations such as, for example, large arrays of coordinates.
Reply
#3

In my opinion, definitions are better, for that type of code.
I think they don't use memory, while static cost use same size memory as normal variables.

I only saw constants in parameters of a function as there we can't use definitions there.

Question: why are you defining costants as static? they can't be changed so...
Reply
#4

Quote:
Originally Posted by Roko_foko
Посмотреть сообщение
In my opinion, definitions are better, for that type of code.
I think they don't use memory, while static cost use same size memory as normal variables.

I only saw constants in parameters of a function as there we can't use definitions there.

Question: why are you defining costants as static? they can't be changed so...
Say that again please, and now with thinking before you say it.
Definitions don't use memory?
Oh really? Explain me why you think that something doesn't use/require resources, I'll be glad to hear.
Reply
#5

Quote:
Originally Posted by Roko_foko
Посмотреть сообщение
Question: why are you defining costants as static? they can't be changed so...
Well, the script which I thought might need some changing if defines are worse in this scenario is an include file and constants should be static in there to avoid clashing. Why am I using static in these examples? No idea.

Anyways, after having a break from thinking about it too much and coming back to it with fresh mind, I found out the answer myself.

In the Snippet thread's examples the definitions are being used as the whole strings and my examples have the definitions being "added" (by the compiler) in formats, which makes this totally different case and obviously defines are better here.

Thread can be closed.
Reply
#6

Quote:
Originally Posted by milanosie
Посмотреть сообщение
Say that again please, and now with thinking before you say it.
Definitions don't use memory?
Oh really? Explain me why you think that something doesn't use/require resources, I'll be glad to hear.
I confess that I am not that strong in that part of programming, you can say what ever you want and I will not be able to say much agaiinst it.

I see definitions work this way.
Код:
#define NUMBER_ONE 1
Once program sees NUMBER_ONE in code it checks #define line and replaces it with the word(s) that are coming after second one(in this case NUMBER_ONE)
You can still think program is using some variables to do that.
Then, I will have to do this code
Код:
#define abs(%0) (%0<0)?(-%0):%0 // I think this code is okay.
in this example, I don't think program will use variables to store the valuse etc. I think, it will just look #define line and replace it with the following code.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)