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: Need to lookup function arguments (in/out) from pdb by dbghelp ...I need to lookup function parameters(their ... to get function parameters as well. As SYMBOL_INFO structure in callback function only contains symbolName,Addresses and Size. Passing va_list by reference to a function - comp.lang.c ...... break; case MOD_Z: u = va_arg(*ap, size_t ... To keep developers who write portable C code on their ... an va_list that was passed to a function as an function argument ... bivariate beta or exponential? - comp.soft-sys.matlab... lets you define the PDFs and their shape/scale parameters ... t2 = sum((repmat(a(1:end-1)-1,size ... la,lap,lb,lbp: Link function applied to the (positive) parameters alpha ... naming convention for class attributes (member data)? - comp.lang ...... as it became fashionable to bash MS for > > their ... stands for "instance" aFoo // for function arguments foo ... know still prefers "static int const size =3D 42 ... image denoising using Adaptive Center-Weighted Median Filter (ACWM ...... parameters delta = [40 25 10 5]; x = double(I); image_size ... function[y, noise_matrix] =3D acwmf(x) % Threshold parameters delta =3D [40 25 10 5]; image_size ... their ... Using C and Assembly code: 64Bit Calling convention - comp.lang ...... and the MS authorities gave their ... as long as you can access your function parameters and ... loads whatever as size and size as address of the compare function. Embedded matlab function in simulink for mutiple inputs and ...Furthermore, one cannot send functions as arguments from ... but it is asking to reframe the size ... Rate Buses to ... By default, function inputs and outputs inherit their data ... strndup: RFC - comp.compilers.lcc... optional arguments I could do: char *strndup(char *str,size_t siz ... in their entirety. I know that the ctype functions are ... know has functions using arguments, and ... Const constructor - comp.lang.c++.moderated... ArraySlice const & s); These two function parameters had different const-qualifications applied, so their ... double sum(const double*, int size); which is one function ... the maximum memory size allowed in malloc - comp.lang.cThe malloc() function expects an argument of type size_t. The maxium size is merely the size of size_t ... Embedded and small systems generally know their own requirements ... Function (mathematics) - Wikipedia, the free encyclopedia... defined by simply tabulating all the arguments x and their corresponding function ... that, when X and Y are finite and of size |X| and |Y|, then the number of functions X → Y ... Functions - gigamonkeys... point of view--what the initial size is. But ... have been given values, if there are any arguments left, their ... histogram of the values returned by the argument function ... 7/10/2012 9:54:39 PM
|