Difference between expansion and contraction in C ++?

What is the difference between expanding and contracting in C ++? What is meant by casting and what are the types of casting?

+1


source to share


3 answers


This is a common thing different from C ++.

"Expanding" casting is casting from one type to another where the "destination" type has a greater range or precision than the "source" (eg int to long, float to double). "Narrowing" is the exact opposite (long for int). The narrowing of the casting allows for overflow.



Expanding casts between inline primitives are implicit, which means you don't need to specify a new type with a cast operator unless you want the type to be treated as a wider type during computation. By default, types are applied to the widest actual type used on the variable side of a binary expression or assignment, not counting any types on the other side).

On the other hand, narrowed drops should be explicitly stripped of, and overflow exceptions should be handled unless the code is marked as unchecked for overflow (the keyword in C # is equal unchecked

; I don't know if it is unique to that language)

+5


source


An expanding transform is when you go from integer to double, you increase the precision of the cast.

narrowing the transform is the opposite when you go from double to integer. You are losing precision



There are two types of castings, implicit and explicit casting. The next page will be helpful. Also the whole site is pretty much suited for c / c ++ needs.

Casting and Conversion Tutorial

+3


source


Take a home exam? :-)

Take casting first. Every object in C or C ++ has a type that is nothing more than a name giving two kinds of information: how much memory a thing takes up and what operations you can do on it.

So,

 int i;

      

means it i

refers to some location in memory, usually 32 bits wide, on which you can do +,-,*,/,%,++,--

some others.

Ci is not very picky, though:

 int * ip;

      

defines another type, called a pointer to an integer, that represents an address in memory. It has an additional option, the prefix - *. On many machines this is 32 bits wide as well.

The listing, or typecast, tells the compiler to treat memory identified as one type as if it were a different type. Typics are written like (typename)

.

So,

 (int*) i;

      

means "treat i

as if it were a pointer, and

 (int) ip;

      

means to treat the pointer ip

as an integer.

Now, in this context, expansion and contraction means casting from one type to another, which has more or fewer bits, respectively.

0


source







All Articles