Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Conversion/Casting Confusion in C++

What is Type Conversion and what is Type Casting?

When should I use each of them?

Detail: Sorry if this is an obvious question; I'm new to C++, coming from a ruby background and being used to to_s and to_i and the like.

like image 483
Ell Avatar asked Dec 02 '10 16:12

Ell


People also ask

What is the difference between type conversion and type casting in C?

1. In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler. 2.

Does C support type casting?

C also allows the programmers to do type casting. Typecasting and Type conversion are different things. In C, typecasting is a way to simply change the data type of a variable to another data type.

What is type conversion in C with example?

Example: Implicit Type Conversion int number = alphabet; Here, the C compiler automatically converts the character 'a' to integer 97. This is because, in C programming, characters are internally stored as integer values known as ASCII Values. ASCII defines a set of characters for encoding text in computers.

What is type casting in C programming?

Typecasting is a method in C language of converting one data type to another. There are two types of typecasting. 1. Implicit Type casting − This conversion is done by the compiler. When more than one data type of variables are used in an expression, the compiler converts data types to avoid loss of data.


5 Answers

Conversion is when a value is, um, converted to a different type. The result is a value of the target type, and there are rules for what output value results from what input (of the source type).

For example:

int i = 3;
unsigned int j;
j = i; // the value of "i" is converted to "unsigned int".

The result is the unsigned int value that is equal to i modulo UINT_MAX+1, and this rule is part of the language. So, in this case the value (in English) is still "3", but it's an unsigned int value of 3, which is subtly different from a signed int value of 3.

Note that conversion happened automatically, we just used a signed int value in a position where an unsigned int value is required, and the language defines what that means without us actually saying that we're converting. That's called an "implicit conversion".

"Casting" is an explicit conversion.

For example:

unsigned int k = (unsigned int)i;
long l = long(i);
unsigned int m = static_cast<unsigned int>(i);

are all casts. Specifically, according to 5.4/2 of the standard, k uses a cast-expression, and according to 5.2.3/1, l uses an equivalent thing (except that I've used a different type). m uses a "type conversion operator" (static_cast), but other parts of the standard refer to those as "casts" too.

User-defined types can define "conversion functions" which provide specific rules for converting your type to another type, and single-arg constructors are used in conversions too:

struct Foo {
    int a;
    Foo(int b) : a(b) {}                   // single-arg constructor
    Foo(int b, int c) : a(b+c) {}          // two-arg constructor
    operator float () { return float(a); } // conversion function
};

Foo f(3,4);              // two-arg constructor
f = static_cast<Foo>(4); // conversion: single-arg constructor is called
float g = f;             // conversion: conversion function is called
like image 128
Steve Jessop Avatar answered Oct 12 '22 23:10

Steve Jessop


Classic casting (something like (Bar)foo in C, used in C++ with reinterpret_cast<>) is when the actual memory contents of a variable are assumed to be a variable of a different type. Type conversion (ie. Boost's lexical_cast<> or other user-defined functions which convert types) is when some logic is performed to actually convert a variable from one type to another, like integer to a string, where some code runs to logically form a string out of a given integer.

There is also static and dynamic casting, which are used in inheritance, for instance, to force usage of a parent's member functions on a child's type (dynamic_cast<>), or vice-versa (static_cast<>). Static casting also allows you to perform the typical "implicit" type conversion that occurs when you do something like:

float f = 3.14; 
int i = f; //float converted to int by dropping the fraction

which can be rewritten as:

float f = 3.14;
int i = static_cast<int>(f); //same thing
like image 36
Ken Simon Avatar answered Oct 12 '22 23:10

Ken Simon


In C++, any expression has a type. when you use an expression of one type (say type S) in a context where a value of another type is required (say type D), the compiler tries to convert the expression from type S to type D. If such an implicit conversion doesn't exist, this results in an error. The word type cast is not standard but is the same as conversion.

E.G.

void f(int x){}

char c; 

f(c); //c is converted from char to int.

The conversions are ranked and you can google for promotions vs. conversions for more details.

There are 5 explicit cast operators in C++ static_cast, const_cast, reinterpret_cast and dynamic_cast, and also the C-style cast

like image 20
Armen Tsirunyan Avatar answered Oct 13 '22 00:10

Armen Tsirunyan


Type conversion is when you actually convert a type in another type, for example a string into an integer and vice-versa, a type casting is when the actual content of the memory isn't changed, but the compiler interpret it in a different way.

like image 45
BlackBear Avatar answered Oct 13 '22 00:10

BlackBear


Type casting indicates you are treating a block of memory differently.

int i = 10;
int* ip = &i;
char* cp = reinterpret_cast<char*>(ip);
if ( *cp == 10 )  // Here, you are treating memory that was declared
{                 // as int to be char.
}

Type conversion indicates that you are converting a value from one type to another.

char c = 'A';
int i = c;  // This coverts a char to an int.
            // Memory used for c is independent of memory
            // used for i.
like image 44
R Sahu Avatar answered Oct 12 '22 22:10

R Sahu