Function arguments and their size

  • Thread starter Martin Johansen
  • Start date
M

Martin Johansen

Hello C programmers

If I have a function, say

void f(int a, char b);

And I call this function with the following arguments

char a;
int b;

f(a, b)

In this and similar cases of passing an argument without typecasting, what
does the C standard say about implicit typecasting in these cases?

thanks!
 
C

Chris Torek

If I have a function, say

void f(int a, char b);

And I call this function with the following arguments

char a;
int b;

f(a, b)

In this and similar cases of passing an argument without typecasting, what
does the C standard say about implicit typecasting in these cases?

As long as there is a prototype in scope, any function call is
semantically equivalent to a sequence of assignments (in no
definable order, perhaps even all simultaneous) to the parameters
to that function:

void f(int, char);

void somefunc(void) {
f(3.14159, 2.71828);
}

means exactly the same thing as:

void somefunc(void) {
f(3, 2);
}

since assigning 3.14159 to an "int" sets that int to 3, and
assigning 2.71828 to a "char" sets that char to 2.

If a prototype is *not* in scope, the actual arguments undergo
the default argument promotions; if the resulting types do not
match the actual formal parameter types[%], the behavior is
undefined. Thus, without a prototype for f(), passing "3.14159"
would pass a double to a function that needs an int, resulting
in undefined behavior. But even:

/* no prototype in scope for f() */
f(3, 2);

or:

/* no prototype in scope for f() */
f(3, (char)'x');

would result in undefined behavior, because f()'s second parameter
must have type "char", and 2 is an int. The second might "look
right", but while (char)'x' is in fact a char, it becomes an int
(or possibly an unsigned int) under the default argument promotions.
-----
[%] The formal parameter types may also undergo promotion if the
function is defined using an old-style K&R definition. That is:

int f(a, b) int a; char b; { ... }

has as its correct prototype:

int f(int, int); /* or possibly int f(int, unsigned int); */

One should always use prototypes in modern C, so as to avoid this
sort of difficult-to-analyze situation.
 
B

Barry Schwarz

Hello C programmers

If I have a function, say

void f(int a, char b);

And I call this function with the following arguments

char a;
int b;

f(a, b)

In this and similar cases of passing an argument without typecasting, what
does the C standard say about implicit typecasting in these cases?
If the function is defined or declared (with prototype) before use,
the compiler will convert each argument to the parameter type (as if
by assignment). If the argument and parameter are incompatible, the
compiler should emit the normal diagnostic and take some
implementation defined action. If the compiler normally produces a
diagnostic on (for example) int to char assignments, you should expect
to see the same when it happens to an argument.

The rules are different for variadic functions but that is a slightly
different topic.


<<Remove the del for email>>
 
J

Jack Klein

Hello C programmers

If I have a function, say

void f(int a, char b);

And I call this function with the following arguments

char a;
int b;

f(a, b)

In this and similar cases of passing an argument without typecasting, what
does the C standard say about implicit typecasting in these cases?

The C standard says nothing about 'implicit typecasting' because there
is no such thing. The phrase is meaningless.

C has conversions between types. Some will happen automatically on
assignment, others can only happen with a cast operator.

A conversion caused by a cast is an 'explicit conversion'. One that
happens without a cast is just a plain old ordinary conversion, not an
'implicit cast'.

In any case, in the presence of a function prototype, arguments are
placed in the function's parameters as if by assignment.

Since the types are directly assignable, automatic and silent
conversion takes place. No cast involved.
 
L

Lawrence Kirby

Martin Johansen wrote on 04/06/05 :

char (and short) parameters don't really exist (they are converted to
int).

In an unprototyped function or a variable argument list, yes, but that's
not the case here. Even with an unprototyped function they value may
be passed as int (or in rare circumstances unsigned int) but the variable
within the called function has its proper declared type.
void f(int a, int b);

So this is not equivalent.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top