From: Ulrich Eckhardt on 13 Jan 2010 02:52 Robby wrote: > Since I have a quite a few variables which must stay as 8 bits, I find > myself having to pass many of these to functions as parameters. > > So if I had: > > int x; > > I am now calling it: > > unsigned char x; > > To me, "x" is still an 8 bit value which actually simulates my old int's. > > However, many of my functions from the 8 bit processor are like this: > > void f1(int x); > > Which means that "x" in the new 32 bit processor is considered as 32 bits > !!!! If you want 8 bits, say that. C99 introduced types via <stdint.h> like int8_t and uint8_t. Use those in the code. If your compiler doesn't have <stdint.h>, create according typedefs as workaround in a central header of your project. > So, my question is, in general, do C/C++ compilers do automatic casting > when a parameter in the calling function is one type, but the function > definition is another. Yes, the types will be implicitly converted. If the conversion is not possible, you will get an error. You should turn on warnings in any case though, some implicit conversions are unhealthy. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Barry Schwarz on 13 Jan 2010 21:46 On Wed, 13 Jan 2010 00:33:51 -0500, "Igor Tandetnik" <itandetnik(a)mvps.org> wrote: >Barry Schwarz wrote: >> There are no type casts in C. The operand of a cast operator is >> always a value. > >That's splitting hairs. "Type cast" is a common colloquial term meaing roughly "an expression involving a cast operator". >In fact, C99 standard uses the term once - see H.2.4p1 (non-normative). > >> (Contrast with the sizeof operator where the operand >> is always a type.) > >Not true. You can write sizeof(expression). Though I don't see how this relates to the issue of type casts. You may write sizeof(expression) but the operand is a type. 6.5.3.4-2 says "The size is determined from the type of the operand." > >> Going back to your original message, there are no automatic casts in >> C. > >But there are implicit type conversions. Sounds like a distinction without a difference to me. > >> A cast occurs only when a cast operator is coded in the >> expression. There are automatic conversions between compatible types >> which is what you described. > >Implicit conversions, if you insist on being pedantic. See the first sentence of 6.3-1. -- Remove del for email
From: Igor Tandetnik on 13 Jan 2010 22:03 Barry Schwarz <schwarzb(a)dqel.com> wrote: > On Wed, 13 Jan 2010 00:33:51 -0500, "Igor Tandetnik" > <itandetnik(a)mvps.org> wrote: > >> Barry Schwarz wrote: >>> (Contrast with the sizeof operator where the operand >>> is always a type.) >> >> Not true. You can write sizeof(expression). Though I don't see how >> this relates to the issue of type casts. > > You may write sizeof(expression) but the operand is a type. 6.5.3.4-2 > says "The size is determined from the type of the operand." If operand is a type, then "type of the operand" becomes "type of the type", which doesn't make much sense. No, the operand is an expression, and the result of sizeof is the size of the type of that expression. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
First
|
Prev
|
Pages: 1 2 Prev: Can not deduse template arguments std::rel_ops Next: Prob with Modal popup dialog. |