Home AMX User Forum AMXForums Archive Threads AMX Applications and Solutions

Constant in DEFINE_CONSTANT with and w/o datatypes

Hi Experts,

I was reviewing AMX code developed by someone else and I come across following piece of code ...

DEFINE_CONSTANT

// VC Screen Modes
VC_Makeacall = 1;
VC_CallProgress = 2;
VC_RecentCalls = 3;
VC_Videoscreen = 4;

// Day and Date
Integer DaySunday = 1;
Integer DayMonday = 2;
Integer DayTuesday = 3;
Integer DayWednesday = 4;
Integer DayThursday = 5;
Integer DayFriday = 6;
Integer DaySaturdday = 7;

Above code is compile piece of code. Can anyone please tell me what is the difference constant without any datatypes and one with datatypes?

Thanks in Advanced.

MC78

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    DEFINE_CONSTANT

    // VC Screen Modes
    VC_Makeacall = 1;
    VC_CallProgress = 2;
    VC_RecentCalls = 3;
    VC_Videoscreen = 4;

    // Day and Date
    Integer DaySunday = 1;
    Integer DayMonday = 2;
    Integer DayTuesday = 3;
    Integer DayWednesday = 4;
    Integer DayThursday = 5;
    Integer DayFriday = 6;
    Integer DaySaturdday = 7;

    Above code is compile piece of code. Can anyone please tell me what is the difference constant without any datatypes and one with datatypes?
    INTEGER
    An intrinsic data type representing a 16-bit unsigned integer. This is the default data type if a non-array variable is declared without a data type specifier.
    Same rule applies for constants so in the example code you posted all those constants will all be interpreted as integers, however, explicitly declaring the data type is the recommended way to go.
  • travtrav Posts: 188
    See that's strange, typing constants has always struck me as something that is just useless, I could be missing the abvious, my wife always says that :)

    But afaik constants are direct substitutions at compile time, so there is no need to type them, you aren't giving the compiler any directives, it is just substituting in what ever you have made them equal to, so there should not be any ambiguity at all in their types as, as I said, they are just subsitituted in line with the rest of the code.

    If anyone has a reason to type their constants can you let me know as I've always been a little confused as to why people do.

    Help me out here!
  • Joe HebertJoe Hebert Posts: 2,159
    trav wrote:
    See that's strange, typing constants has always struck me as something that is just useless, I could be missing the abvious, my wife always says that :)
    My wife always says that I?m strange so maybe we?re even. :)
    trav wrote:
    If anyone has a reason to type their constants can you let me know as I've always been a little confused as to why people do.
    If you try to do something like this?
    DEFINE_CONSTANT
    nVolumePresetButtons[]	= {301,302,303,304}	
    
    ?then you will get a converting type integer to char warning and the values of 301-304 will end up being 45-48. If you want a constant integer array then you have to explicitly declare the type like this:
    DEFINE_CONSTANT
    INTEGER nVolumePresetButtons[]	= {301,302,303,304}
    
    Or if you try to do something like this:
    DEFINE_CONSTANT
    snVolumePresetMap[]	= {-275,-150,-25,100}	
    
    ?then you will also get a converting type warning and the negative values intended will not be so unless you do this instead:
    DEFINE_CONSTANT
    SINTEGER snVolumePresetMap[]	= {-275,-150,-25,100}	
    

    So if you want to use anything other than single integers or char arrays as constants then the type has to be declared. I suppose I could just type the constants that don?t fit the defaults but I personally feel it?s better to type them all. Perhaps my wife is correct and I am indeed strange. :)
  • DHawthorneDHawthorne Posts: 4,584
    trav wrote: »
    See that's strange, typing constants has always struck me as something that is just useless, I could be missing the abvious, my wife always says that :)

    But afaik constants are direct substitutions at compile time, so there is no need to type them, you aren't giving the compiler any directives, it is just substituting in what ever you have made them equal to, so there should not be any ambiguity at all in their types as, as I said, they are just subsitituted in line with the rest of the code.

    If anyone has a reason to type their constants can you let me know as I've always been a little confused as to why people do.

    Help me out here!

    My primary use of DEFINE_CONSTANT is to "name" array indexes so I know what the heck I was doing when I look at it two months later. For example:
    DEFINE_DEVICE
    
    dvTP1 = 10001:1:0
    dvTP2 = 10002:1:0
    
    DEFINE_CONSTANT
    
    KITCHEN_PANEL = 1
    FAMILY_PANEL = 2
    
    DEFINE_VARIABLE
    
    VOLATILE DEV dvHousePanels[] = {dvTP1, dvTP2} 
    
    Now, whenever I need to work with dvHousePanels, I can refer to it as dvHousePanels[KITCHEN_PANEL] and know right away which one I am calling, where dvHousePanels[1] may not be so obvious. I'll do something similar with multi-room audio zones, inputs, just about anything I may need a lookup table for. It also makes it a lot easier to change later ... if your zones are in an array, and the configuration changes, you only have to change the values in DEFINE_CONSTANT, not go through every line of code to make sure you didn't miss any.
  • jweatherjweather Posts: 320
    trav wrote: »
    See that's strange, typing constants has always struck me as something that is just useless, I could be missing the abvious, my wife always says that :)

    But afaik constants are direct substitutions at compile time, so there is no need to type them, you aren't giving the compiler any directives, it is just substituting in what ever you have made them equal to, so there should not be any ambiguity at all in their types as, as I said, they are just subsitituted in line with the rest of the code.

    If anyone has a reason to type their constants can you let me know as I've always been a little confused as to why people do.

    Help me out here!

    Constants are definitely typed, they aren't macro substitutions like a #define in C. You can't use them to substitute arbitrary strings or expressions in your program. Just like variables, though, the INTEGER part is assumed if you don't mention it (unless you put in a string/array like Joe said). Since most people use only integer constants, though, it frequently gets omitted (harmlessly).
  • Don't know about the obvious, but you're definitely missing the spell check button. :)

    - Chip
    trav wrote: »
    I could be missing the abvious
  • travtrav Posts: 188
    Gud Inglish

    Dare Aint nufink wrong wif my spelink.
  • jweather wrote: »
    Constants are definitely typed, they aren't macro substitutions like a #define in C. You can't use them to substitute arbitrary strings or expressions in your program. Just like variables, though, the INTEGER part is assumed if you don't mention it (unless you put in a string/array like Joe said). Since most people use only integer constants, though, it frequently gets omitted (harmlessly).

    I have the following code that seems to contradict your statement about using constants as string expressions.
    DEFINE_CONSTANT
    
    GARAGE1  = 'North Door'	// Define the name for the door connected to Relay 1 and IO 1
    GARAGE2  = 'South Door'	// Define the name for the door connected to Relay 2 and IO 2
    
    
    DEFINE_TYPE
    
    STRUCTURE GARAGE_DOOR
    {
       CHAR NAME[30]
       DEVCHAN DC
    }
    
    
    
    DEFINE_START
    
    #IF_DEFINED GARAGE1
       GarageDoors[1].NAME = "GARAGE1"
       GarageDoors[1].DC.DEVICE = dvIO
       GarageDoors[1].DC.CHANNEL = 1
    #END_IF
    #IF_DEFINED GARAGE2
       GarageDoors[2].NAME = "GARAGE2"
       GarageDoors[2].DC.DEVICE = dvIO
       GarageDoors[2].DC.CHANNEL = 2
    #END_IF
    

    This code works just as I expected it to. When I view the string GarageDoors[1].NAME it's value is North Door.
  • jweatherjweather Posts: 320
    The result is the same as if GARAGE1 was declared in DEFINE_VARIABLE -- it's automatically assigned a type of char[], and it gets substituted in the string expression the same as a variable. Constants are just read-only variables.

    Macro substitution (aka global search and replace, a la C's #define pragma) would let you do useful things like
    // NOTE: This is NOT how it works
    DEFINE_CONSTANT
    
    LastTPName = strTPName[GET_LAST(dvTPs)]
    
    DEFINE_EVENT
    
    BUTTON_EVENT [dvTPs, 1] {
      PUSH: {
        SEND_STRING 0, "'TP name is ', LastTPName"
      }
    }
    
  • JeffJeff Posts: 374
    Constants are by default char types, not integer types. Thats why you have to explicitly declare them as integers when you want to use them for a button array.

    And thats why North Door works the way it does. You can declare a constant as 'YOUR MOM' with no problem, but you can't declare one as {1,3,3,7} without declaring it as an integer
  • matt95gsrmatt95gsr Posts: 165
    What you say here is actually only true for arrays, just like jweather mentioned. You can declare a constant as 5, and it knows it's an integer, but not {1,3,3,7} without specifying that it is to be an integer array. Likewise, you can declare a constant as 'North Door' without specifying type and it will be a char array, but you cannot declare a constant 'N' without specifying that it is a char. Same way as variable declarations, they are assumed to be integers by default, or chars by default if you declare an array.
    Jeff wrote: »
    Constants are by default char types, not integer types. Thats why you have to explicitly declare them as integers when you want to use them for a button array.

    And thats why North Door works the way it does. You can declare a constant as 'YOUR MOM' with no problem, but you can't declare one as {1,3,3,7} without declaring it as an integer
  • wengerpwengerp Posts: 29
    Macro substitution
    jweather wrote: »
    Macro substitution (aka global search and replace, a la C's #define pragma) would let you do useful things like
    // NOTE: This is NOT how it works
    DEFINE_CONSTANT
    
    LastTPName = strTPName[GET_LAST(dvTPs)]
    
    DEFINE_EVENT
    
    BUTTON_EVENT [dvTPs, 1] {
      PUSH: {
        SEND_STRING 0, "'TP name is ', LastTPName"
      }
    }
    

    Although not a real macro substitution for C style macros, using the #DEFINE compiler directive the following code is actually working:
    PROGRAM_NAME='DEFINE_SAMPLE'
    
    DEFINE_DEVICE
    dvTP = 10001:10:0
    
    DEFINE_VARIABLE
    
    volatile DEV dvTPs[] = { dvTP }
    volatile char strTPName[1][3] = { 'TP1' }
    
    #DEFINE LastTPName strTPName[GET_LAST(dvTPs)]
    
    DEFINE_EVENT
    
    BUTTON_EVENT [dvTPs, 1] {
      PUSH: {
        SEND_STRING 0, "'TP name is ', LastTPName"
      }
    }
    

    However, it is not possible to use dynamic substitutions using variables as it would be possible in C.
    Patrick
Sign In or Register to comment.