Function arguments and their size

  • Follow


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! 


0
Reply mfag (26) 6/3/2005 11:47:49 PM

In article <a%5oe.11946$SL4.264385@news4.e.nsc.no>
Martin Johansen <mfag@online.no> wrote:
>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.
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
0
Reply nospam252 (1722) 6/4/2005 4:13:11 AM


On Sat, 4 Jun 2005 01:47:49 +0200, "Martin Johansen" <mfag@online.no>
wrote:

>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>>
0
Reply schwarzb (662) 6/4/2005 4:50:01 AM

On Sat, 4 Jun 2005 01:47:49 +0200, "Martin Johansen" <mfag@online.no>
wrote in comp.lang.c:

> 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.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
0
Reply jackklein (3932) 6/4/2005 5:02:13 AM

Martin Johansen wrote on 04/06/05 :
> Hello C programmers
>
> If I have a function, say
>
> void f(int a, char b);

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

void f(int a, int b);

What was your question again ?

-- 
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

0
Reply emdel (952) 6/4/2005 10:07:00 AM

On Sat, 04 Jun 2005 12:07:00 +0200, Emmanuel Delahaye wrote:

> Martin Johansen wrote on 04/06/05 :
>> Hello C programmers
>>
>> If I have a function, say
>>
>> void f(int a, char b);
> 
> 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

0
Reply lknews (877) 6/4/2005 11:59:01 AM

5 Replies
46 Views

(page loaded in 0.1 seconds)

Similiar Articles:













7/10/2012 9:54:39 PM


Reply: