Passing a hexadecimal number to a function

gcc 4.4.2 c89

I have it defined in our API header file.

/* Reason for releasing call */ #define UNASSIGNED_NUMBER 0x01 /* Number unassigned / unallocated */ #define NORMAL_CLEARING 0x10 /* Call dropped under normal conditions*/ #define CHANNEL_UNACCEPTABLE 0x06 #define USER_BUSY 0x11 /* End user is busy */ . . . 

However, I want to pass it to functions, but I'm not sure about the type. Could I pass as an integer value? The release_call function takes one of them as a parameter. However, I'm not sure if definitions are defined as hexadecimal notation.

 drop_and_release_call(23, 6); /* Dropping call for CHANNEL_UNACCEPTABLE */ uint32_t drop_and_release_call(uint32_t port_num, uint32_t reason) { release_call(port_num, reason); } 

Thanks so much for any suggestions,

+4
source share
6 answers

Yes, it's just an integer.

The hexadecimal aspect of this question is important only when it is displayed to us.

+4
source

0x06 and 6 (and CHANNEL_UNACCEPTABLE ) are equivalent. So 0x11 and 17 (and USER_BUSY ). There is no difference in the hexadecimal or decimal value on the computer.

(For clarity, you should write drop_and_release_call(23, CHANNEL_UNACCEPTABLE) .)

+6
source

The reason for these definitions is that you can call your function as follows:

 drop_and_release_call(23, CHANNEL_UNACCEPTABLE); 

If you really want to use numbers, yes, you can just pass them directly:

 drop_and_release_call(23, 0x06); 
+3
source

Yes, you can pass it as integers or unsigned integers. These names are replaced by the C preprocessor to become the numbers that they define. These numbers are valid integer literals.

Usually it is customary to introduce "enumerated" types (for example, port number) without a sign. Flags too.

+2
source

C doesn't care if it was in code as base 16 or base 10 ... it will represent it as a whole anyway.

The only time you really need to worry about databases is during input or output.

+2
source

The C compiler will parse the values ​​as an int. For the values ​​you specified, you need only 5 bits, this is convenient in char. But in any case, char gets the int value in the function call, you can also use int.

+1
source

Source: https://habr.com/ru/post/1299811/


All Articles