ant2009
ant2009

Reputation: 22566

passing a hex number to a function

gcc 4.4.2 c89

I have these defines 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 one to a function but I am not sure of the type. I could pass as just an integer value? The release_call function takes one of these as its parameter. However, I am not sure as the defines are defined as hex 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);
}

Many thanks for any suggestions,

Upvotes: 4

Views: 15817

Answers (6)

Hans Passant
Hans Passant

Reputation: 942020

The C compiler will parse the values as int. You need only 5 bits for the values you've listed, it will fit comfortably in a char. But a char get promoted to an int in a function call anyway, you might as well use int.

Upvotes: 1

John Mulder
John Mulder

Reputation: 10095

C doesn't care whether it was in the code as base 16 or base 10... it will represent it as integer either way.

The only time you really have to worry about bases is during input or output.

Upvotes: 2

pavium
pavium

Reputation: 15118

Yes, it's just an integer.

The hexadecimal aspect of it is only important when it's displayed for us to read.

Upvotes: 4

Eli Bendersky
Eli Bendersky

Reputation: 273636

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

It is customary to make "enumerative" types (like port number) unsigned. Flags too.

Upvotes: 2

Carl Norum
Carl Norum

Reputation: 225042

The reason those definitions are made is so that you can call your function like this:

drop_and_release_call(23, CHANNEL_UNACCEPTABLE);

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

drop_and_release_call(23, 0x06);

Upvotes: 3

kennytm
kennytm

Reputation: 523514

0x06 and 6 (and CHANNEL_UNACCEPTABLE) are equivalent. So is 0x11 and 17 (and USER_BUSY). There is no distinction of hexadecimal or decimal to the computer.

(You should write drop_and_release_call(23, CHANNEL_UNACCEPTABLE) for clarity.)

Upvotes: 6

Related Questions