tags:

views:

122

answers:

3

hi.... difference between widening and narrowing in c++? what is mean by casting and what is types of casting?

+1  A: 

widening conversion is when you go from a integer to a double, you are increasing the precision of the cast.

narrowing conversion is the inverse of that, when you go from double to integer. You are losing precision

There are two types of casting , implicit and explicit casting. The page below will be helpful. Also the entire website is pretty much the goto for c/c++ needs.

Tutorial on casting and conversion

george9170
There is no such thing as a `decimal` in C++; do you mean "floating-point type"? Moving between integral types and floating-point types is not generally treated as widening or narrowing.
David Thornley
i ment to write in double, and thought i edited the post much earlier to reflect that.
george9170
@george9170: And you can still lose precision going from an integral type to a double.
David Thornley
for example? Also i believe the original question asked what is widening and narrowing conversion. This are examples generalizations. Theres always an exception to the rule.
george9170
+3  A: 

This is a general casting thing, not C++ specific.

A "widening" cast is a cast from one type to another, where the "destination" type has a larger range or precision than the "source" (e.g. int to long, float to double). A "narrowing" cast is the exact opposite (long to int). A narrowing cast introduces the possibility of overflow.

Widening casts between built-in primitives are implicit, meaning you do not have to specify the new type with the cast operator, unless you want the type to be treated as the wider type during a calculation. By default, types are cast to the widest actual type used on the variable's side of a binary expression or assignment, not counting any types on the other side).

Narrowing casts, on the other hand, must be explicitly cast, and overflow exceptions must be handled unless the code is marked as not being checked for overflow (the keyword in C# is unchecked; I do not know if it's unique to that language)

KeithS
C++ casting is specific to C++, and the standard widening where, for example, you're multiplying a `short` by a `long` isn't referred to as casting in C or C++. Your last paragraph is completely inapplicable to C++.
David Thornley
To build on David Thornley: there is no check for overflow on narrowing in C++. It's a shame, but that's the way it is.
Matthieu M.
Also, whether int-to-long or long-to-int involves widening or narrowing is platform dependent. sizeof(int) <= sizeof(long).
dgnorton
+2  A: 

Take home exam? :-)

Let's take casting first. Every object in C or C++ has a type, which is nothing more than the name give to two kinds of information: how much memory the thing takes up, and what operations you can do on it.

So

 int i;

just means that i refers to some location in memory, usually 32 bits wide, on which you can do +,-,*,/,%,++,-- and some others.

Ci isn't really picky about it, though:

 int * ip;

defines another type, called pointer to integer which represents an address in memory. It has an additional opertion, prefix-*. On many machines, that also happens to be 32 bits wide.

A cast, or typecast tell the compiler to treat memory identified as one type as if it were another type. Typecasts are written as (typename).

So

 (int*) i;

means "treat i as if it were a pointer, and

 (int) ip;

means treat the pointer ip as just an integer number.

Now, in this context, widening and narrowing mean casting from one type to another that has more or fewer bits respectively.

Charlie Martin