473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Function arguments and their size

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!
Nov 14 '05 #1
5 1306
In article <a%********************@news4.e.nsc.no>
Martin Johansen <mf**@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.
Nov 14 '05 #2
On Sat, 4 Jun 2005 01:47:49 +0200, "Martin Johansen" <mf**@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>>
Nov 14 '05 #3
On Sat, 4 Jun 2005 01:47:49 +0200, "Martin Johansen" <mf**@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/~a...FAQ-acllc.html
Nov 14 '05 #4
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.

Nov 14 '05 #5
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

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
0
by: EasyRider41 | last post by:
I am trying to merge to scripting samples I for on a source code web site and having limited luck. Then first one is called Zebra Tables witch colors alternate rows of a table to look beter. The...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
38
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
7
by: Icario | last post by:
I have an error when i compile this: class A { public: void f(A&) {} }; class B: public A { public: void f(B&) {}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.