Question:
what do u mean by type casting ?
2007-08-11 01:30:26 UTC
what do u mean by type casting ?
Ten answers:
oldguy
2007-08-11 16:15:12 UTC
Lets see if I can add to or simplify what has already been said.

One way that modern compilers help the programmer avoid mistakes is by enforcing types. Just one example, if you try to store a double value into an integer variable, like:

int ii = 32.5 * x;

you might get a warning like "storing a double into an integer may cause loss of data" since any fractional part would be lost. If the programmer had used the wrong variable, or forgot to change one to be a 'double' this warning might save hours of debugging. But what if the programmer *intended* to do that. In this case, the programmer notifies the compiler of intention to do something that might be considered a mistake, by performing a typecast:

int ii = (int)(32.5 * x);

which says "I want the result of the multiplication to be handled as an 'int'"

By telling the compiler explicitly what is intended, the compiler assumes you know what you are doing and does not display the warning.

Some typecasts can not be interpreted meaningfully because the types involved are too dissimilar. For example:

(ComplexPointArray)1

*or*

(int)"blue"

In these cases, the compiler will still generate a warning.



Hopes this helps your understanding.
angel04
2007-08-11 09:40:16 UTC
Type Casting

In any application, there may be many situations in which one data type may need to be converted to another data type.



There are two types of conversion: implicit and explicit.



Implicit casting means simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not workout for all application scenarios.



Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler.



Example: java code



int t = 100;



long h = t; //Implicit casting



For example, the following is legal C language code: for implicit type conversion



double d;

long l;

int i;



if (d > i) d = i;

if (i > l) l = i;

if (d == l) d *= 2;

Although d, l and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. This behavior should be used with caution, as unintended consequences can arise. Data can be lost when floating-point representations are converted to integral representations as the fractional components of the floating-point values will be truncated (rounded down). Conversely, converting from an integral representation to a floating-point one can also lose precision, since the floating-point type may be unable to represent the integer exactly (for example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can). This can lead to situations such as storing the same integer value into two variables of type int and type single which return false if compared for equality.



Explicit

The most common form of explicit type conversion is known as casting. Explicit type conversion can also be achieved with separately defined conversion routines



Explicit casting means very specifically informing the compiler about the transformation that is expected.



long h = 100.00;



t = (int) h; //Explicit casting



Whenever you are trying to do implicit casting in those scenarios in which you are supposed to use explicit casting, the compiler will throw an exception like this:



Incompatible type for =. Explicit cast needed to convert long to int.



We will try understand these two in greater detail, in the process of casting fundamental data types as well as casting objects which is a bit more tactical.



There are several kinds of explicit conversion.



checked

Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.

unchecked

No check is performed. If the destination type cannot hold the source value, the result is undefined.

bit pattern

The data is not interpreted at all, and its raw bit representation is copied verbatim. This can also be achieved via aliasing.



Each programming language has its own rules on how types can be converted. In general, both objects and fundamental data types can be converted





// class type-casting

#include

using namespace std;



class CDummy {

float i,j;

};



class CAddition {

int x,y;

public:

CAddition (int a, int b) { x=a; y=b; }

int result() { return x+y;}

};



int main () {

CDummy d;

CAddition * padd;

padd = (CAddition*) &d;

cout << padd->result();

return 0;

}
Dave H
2007-08-11 02:03:56 UTC
You may have an object that has no type specified or the wrong type specified for a needed function.



For example an animal could be an object that is passed to a function.



You may need to cast the animal object into a dog in order to call the dog.bark() function. The dog object would have to be derived from the animal object for the type cast to work properly.
abhishek t
2007-08-11 01:41:48 UTC
type casting in comp science means to convert from another data type



suppose u have taken a variable of integer data type and u r dividing and getting the answers in float u have to use type cast here either the result will be shown in integer only



type casting can be implicit and explicit
Sippy
2007-08-11 11:32:39 UTC
hi,

Listen, type casting is the procedure to convert the data from one type to another in any programming language, but type casting is possible only in the same category of data like in C or C++ , you can type cast int to char, coz all chars r in 0-255 and these are numbers and these numbers are in range of int (0-32767).

thats why you can convert/(type cast ) these types of datas

also int to long, float to int, etc.

if u convert float to int , then its decimal area would be removed, coz 'int' which is accepting the resulting value is not able to accept points' values.

so dear now did u understand?

you can ask me any problem anytime,

My contact is : surjitsippy@yahoo.com

Yahoo messenger ID : surjitsippy

and Cell : +91-98556-30708, +91-98784-30708, +1-315-836-4356

Thanks

Surjeet S.
petroni
2016-09-30 04:02:50 UTC
Typecasting Definition
2007-08-11 21:36:19 UTC
Type -Casting is what it is ....Casting (or converting or moulding) of value of one Data type into the value of another data type.....

in any progaming language....no two data types have same storage size........So when we convert one data type value into other..actually the storage size of the value changes....that is....the value is moulded( or CASTED) into size of other data type...this means type-casting is CASTING OF TYPE(of Data)





but BE CAREFUL......if values of data types which have higher storage size.....are casted into data types with lower storage size....then data loss is bound to occur...this data loss is actually loss of bits.....which may or not affect your data value......



eg(in C/C++):--



int I = 8;

short S = I; // Bits will be lost but no effect on data value..as 8 // can be stored in four bits and 'short' type has 8-bit storage.....



I = 500;



short S1 = I; // bits will be lost and also data value will change

// as 500 requires more than just 8-bits to store..and size of

// 'short' type is only 8 bits.....
♥Remi♥
2007-08-11 01:36:00 UTC
Conversion from one data type to another is called type casting. There are both implicit and explicit conversions. Check out the link below



http://www.cplusplus.com/doc/tutorial/typecasting.html
Joe_Young
2007-08-11 01:37:15 UTC
Pooja beti if you are talking about type casting then you are talking about C++



The word typecasting (past participle typecast) can mean more than one thing



type conversion in computer programming

type conversion in aviation

typecasting (acting) in acting

Typecast, a Filipino band



try this link



http://en.wikipedia.org/wiki/Wikipedia:Disambiguation
2007-08-11 19:32:30 UTC
http://www.instructables.com/id/E6QSBN2Z30ES9J4834/



see this url it will explain about the casting


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...