ch3-StaticCast in C/C++

Chapter_3     Exercise_3-14     SimpleCast ConstCast     Exercise_3-15







StaticCast     TCP1, p. 182-183  (staticast.c,  StaticCast.cpp)




staticast.c         download


#include <stdio.h> // for printf()

void func(int i) {}

int main()
{
int i = 0x7fff; // 0x00007fff (32767)
long l;
float f;

// (1) Typical castless conversions:
l = i;
f = i;
// Also works:
l = (long)i; // explicit cast
f = (float)i; // explicit cast

// (2) Narrowing conversions:
i = l; // May lose digits (automatic narrowing cast)
i = f; // May lose info (automatic narrowing cast)
// Says "I know," eliminates warnings:
i = (int)l; // narrowing cast
i = (float)f; // narrowing cast
char c = (char)i; // 0xff, last byte of i: 11111111 (narrowing cast)
c = i; // automatic narrowing cast
printf("i = %d\n", i); // 32767
printf("(int)c = %d\n", (int)c); // -1 (binary 11111111)
printf("(unsigned int)c = %u\n", (unsigned int)c); // UINT_MAX (limits.h)
unsigned char uc = i; // 0xff, last byte of i (automatic narrowing cast)
printf("(int)uc = %d\n", (int)uc); // 255, binary 11111111 (CHAR_MAX)

// (3) Forcing a conversion from void* :
void* vp = &i;
float* fp = (float*)vp; // dangerous conversion

// (4) Implicit type conversions, normally performed by the compiler:
double d = 0.0;
int x = d; // Automatic type conversion (narrowing)
x = (int)d; // More explicit
func(d); // Automatic type conversion (narrowing)
func((int)d); // More explicit
}
/*
gcc staticast.c -o staticast
./staticast
i = 32767
(int)c = -1
(unsigned int)c = 4294967295 // UINT_MAX
(int)uc = 255 // CHAR_MAX
*/











StaticCast.cpp         download


#include <iostream>
using std::cout;
using std::endl;

void func(int) {} // C requires a named parameter, as in `int i'

int main()
{
int i = 0x7fff; // 0x00007fff (32767)
long l;
float f;

// (1) Typical castless conversions:
l = i;
f = i;
// Also works:
l = static_cast<long>(i);
l = (long)i; // C style cast
f = static_cast<float>(i);
f = (float)i; // C style cast

// (2) Narrowing conversions:
i = l; // May lose digits (automatic narrowing cast)
i = f; // May lose info (automatic narrowing cast)
// Says "I know," eliminates warnings:
i = static_cast<int>(l);
i = (int)l; // C style cast (narrowing)
i = static_cast<int>(f);
i = (float)f; // C style cast (narrowing)
char c = static_cast<char>(i); // 0xff, last byte of i: 11111111
cout << "i = " << i << endl; // 32767
cout << "(int)c = " << (int)c << endl; // -1 (binary 11111111)
cout << "(unsigned int)c = " << (unsigned int)c << endl; // UINT_MAX
c = (char)i; // C style cast (narrowing)
c = i; // automatic narrowing cast
unsigned char uc = i; // 0xff (255), last byte of i: 11111111
cout << "(int)uc = " << (int)uc << endl; // 255 (CHAR_MAX, see <climits>)

// (3) Forcing a conversion from void* :
void* vp = &i;
// Old way produces a dangerous conversion:
float* fp = (float*)vp;
// The new way is equally dangerous:
fp = static_cast<float*>(vp);

// (4) Implicit type conversions, normally performed by the compiler:
double d = 0.0;
int x = d; // Automatic type conversion (narrowing)
x = static_cast<int>(d); // More explicit
x = (int)d; // C style cast
func(d); // Automatic type conversion (narrowing)
func(static_cast<int>(d)); // More explicit
func((int)d); // More explicit, C style
}
/*
g++ StaticCast.cpp -o StaticCast
./StaticCast
i = 32767
(int)c = -1
(unsigned int)c = 4294967295 // UINT_MAX
(int)uc = 255 // CHAR_MAX
*/









Chapter_3     Exercise_3-14     SimpleCast BACK_TO_TOP ConstCast     Exercise_3-15



Comments

Popular posts from this blog

Contents