Max Z
Max Z

Reputation: 107

Passing uint8_t instead of uint32_t into a function (in C)

Going over some code (written in the C language) I have stumbled upon the following:

//we have a parameter

uint8_t num_8 = 4;

//we have a function

void doSomething(uint32_t num_32) {....}

//we call the function and passing the parameter

doSomething(num_8);

I have trouble understanding if this a correct function calling or not. Is this a casting or just a bug?

In general, I know that in the C / C++ languages only a copy of the variable is passed into the function, however, I am not sure what is actually happening. Does the compiler allocates 32 bits on the stack (for the passed parameter), zeros all the bits and just after copies the parameter?

Could anyone point me to the resource explaining the mechanics behind the passing of parameter?

Upvotes: 2

Views: 2573

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148900

As the function declaration includes a parameter type list, the compiler knows the type of the expected parameter. It can then implicitely cast the actual parameter to the expected type. In fact the compiler processes the function call as if it was:

doSomething((uint32_t) num_8);

Old progammers that once used the (not so) good K&R C can remember that pre-ansi C only had identifier lists and the type of parameters was not explicitely declared. In those day, we had to explicitely cast the parameters, and when we forgot, we had no compile time warnings but errors at execution time.

The way the cast and the call is precisely translated in assembly language or machine instruction is just an implementation detail. What matters is that everything must happen as if the compiler had declared a temporary uint32_t variable had coerced the uint8_t value into the temporary and had passed the temporary to the function. But optimizing compilers often use a register to pass a single integer parameter. Depending on the processor, it could zero the accumulator, load the single byte in the low bits and call the function.

Upvotes: 3

Related Questions