473,757 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1327
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.l earn.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
4964
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
14946
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
0
1812
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 second is a rule code that highlights the row you are currently moused over. Both are making use of CSS style sheet to do all of their formating. My problem is that the rollover is changing the text color but not the color of the row background as...
39
6550
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. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
7
23555
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. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
35
2462
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 time in considering this post, and for your comments. I wrote this function to simplify the task of combining strings using mixed sources. I often see the use of sprintf() which results in several lines of code, and more processing than really...
64
3397
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 "refactor-ed" to be
38
2380
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
28
4336
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
1469
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&) {}
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.