Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Explanation of the #define command

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

Anyone can help me to explain #define at bellow:

#define KEYPAD_NO_NEW_DATA (char) '-'

Thanks
 

#define

When the compiler will found KEYPAD_NO_NEW_DATA string in your files will replace it with the ASCII code for - (just the last byte if are more then 1, due the type cast to char).
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: #define

marie65 said:
When the compiler will found KEYPAD_NO_NEW_DATA string in your files will replace it with the ASCII code for - (just the last byte if are more then 1, due the type cast to char).

Hi,

I still not understand what you mean just the last byte if are more then 1, due the type cast to char can you explain clearly bcause i am beginner.. :) .....

Why they put (char) and '-', what they define?? If they
#define TRAN_BUFFER_LENGTH 20, i know the TRAN_BUFFER_LENGTH is 20

What the different between:
#define TRAN_BUFFER_LENGTH 20 and int TRAN_BUFFER_LENGTH = 20

Thanks You..
 

#define

#define is use just to easy modify and understand your code. Practical when you #define A B , at compile time the A will be replaced with B. Now how complicated is the #define is a code writer option.
first:
'-' means the ASCII code for the character - , that is 0x2D (or something near). If the ASCII codes are coded in integers, that means that you can have 65535 ASCII codes, then the type cast (char) will select only 0x2D, and replace your definition in the code with 0x2D, instead of 0x002D. If your ASCII code is coded on 1 byte (that means max 256 ASCII codes) then your type cast to char is redundant and don't do any harm.
second:
the diference betwen #define TRAN_BUFFER_LENGHT 20 and int TRAN_BUFFER_LENGHT = 20 is that:
in first case whenever TRAN_BUFFER_LENGHT is found in your code will be replaced with 20 at compile time
in the second case you define ("create" to be less ambiguos) an integer variable call TRAN_BUFFER_LENGHT and initialise to value 20
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: #define

Hi,

Q1)
I already understand it already.. which mean that..
if we put (char) it just only 8bit=256=>0xFF, so in ASCII the - is 0x2D then the TRAN_BUFFER_LENGHT is define as a 0x2D = -, am i right??

if we put (int) it have 16bit=65535=>0xFFFF, then the TRAN_BUFFER_LENGHT is define as a 0x002D = -, am i right??

Q2)
In what condition we must put (int) / (long)..? Since the ASCII just only use 8bit(char)...

Q3)
I understand that the int TRAN_BUFFER_LENGTH = 20 is define TRAN_BUFFER_LENGTH as a int and it can define between this 0 - 65535 value. If we compare with #define TRAN_BUFFER_LENGTH 20 it still given us the same result am i right...? Can we alway use define ... define... and define... all the ... eg. #define BUFFER_LENGTH 20, #define BUFFER 30 and so on..?

Thanks...
 

Re: #define

Q1)
Yes, but that's not the point. See Q2

Q2)

see https://www.unicode.org/ . The characters are coded on 16 bits.

Q3)

Yes, but the variable definition (int TRAN_BUFFER_LENGTH = 20) can be change at run time. The #define is seen as a constant.
No, we can't use always define.. define.., with the exception (unlikely) that your code don't use variables.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: #define

marie65 said:
Q1)
Yes, but that's not the point. See Q2

Q2)

see h**p://www.unicode.org/ . The characters are coded on 16 bits.

Q3)

Yes, but the variable definition (int TRAN_BUFFER_LENGTH = 20) can be change at run time. The #define is seen as a constant.
No, we can't use always define.. define.., with the exception (unlikely) that your code don't use variables.

Hi,

Q1,2)
I understand already..., there are many type of code ASCII, UCS, UTF.. and so on.

Type Each character
encoded as Notes
7-bit a single 7-bit quantity example: ISO 646
8-bit G0/G1 a single 8-bit quantity with constraints on use of C0 and C1 spaces
8-bit a single 8-bit quantity with no constraints on use of C1 space
8-bit EBCDIC a single 8-bit quantity with the EBCDIC conventions rather than ASCII conventions

16-bit (UCS-2) a single 16-bit quantity within a code space of 0..FFFF
32-bit (UCS-4) a single 32-bit quantity within a code space 0..7FFFFFFF
32-bit (UTF-32) a single 32-bit quantity within a code space of 0..10FFFF
16-bit DBCS process code a single 16-bit quantity example: UNIX widechar implementations of Asian CCS's

32-bit DBCS process code a single 32-bit quantity example: UNIX widechar implementations of Asian CCS's

If we use ASCII it just the 8bit only, if we use UCS then it is 16bit but the program is difference writing already, not that simple #define KEYPAD_NO_NEW_DATA (char) '-' already... am i right?


Q3)
Yes, but the variable definition (int TRAN_BUFFER_LENGTH = 20) can be change at run time
You mean that, eg:
Code:
int i=10;
for(i=0; i<10; i++)
{

}
The i will be changing all the time when program is run... So, is we..

Code:
#define i 10
.
.

for(i=0; i<10; i++)
{

}
The i define in this way is a ERROR because the i is the constant .... am i right??

Thanks..
 

#define

Yes, you are right
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top