Why use static_cast<int>(x) instead of (int)x in C++?



The (int)x is C-style typecasting, where static_cast<int>(x) is used in C++. This static_cast<>() gives a compile-time checking facility, but the C-style casting does not support that. This static_cast<>() can be spotted anywhere inside a C++ code. And using this C++ cast, the intentions are conveyed much better.

In C like cast, sometimes we can cast some type pointer to a point some other type data. Like one integer pointer can also point character type data, as they are quite similar, the only difference is character has 1-byte, integer has 4-bytes. In C++, the static_cast<>() is more strict than C-like casting. It only converts between compatible types.

char c = 65; //1-byte data. ASCII of 'A'
int *ptr = (int*)&c; //4-byte

Since in a 4-byte pointer, it is pointing to 1-byte of allocated memory, it may generate runtime error or will overwrite some adjacent memory.

In C++ the static_cast<>() will allow the compiler to check whether the pointer and the data are of same type or not. If not it will raise incorrect pointer assignment exception during compilation.

char c = 65; //1-byte data. ASCII of 'A'
int *ptr = static_cast<int>(&c);

This will generate a compile-time error.

Approaches to understand the C-Style Cast and static_cast

Let's go through the approaches for better understanding:

Basic Type Casting

The basic type casting shows how a float can be converted to int using both C-style and static_cast.

Syntax

The following is the syntax for the C-style cast and C++ static_cast:

// C-style cast
int x = (int)3.14;
// C++ static_cast
int y = static_cast<int>(3.14);

Example

By using both C-style and C++ static_cast, converting a float to an integer.

#include <iostream>
using namespace std;
int main() {
   float f = 3.14;
   int a = (int)f; // C-style cast
   int b = static_cast<int>(f); // C++ cast
   cout << "C-style cast result: " << a << endl;
   cout << "static_cast result: " << b << endl;
   return 0;
}

Output

The above program produces the following result :

C-style cast result: 3
static_cast result: 3

Pointer Typecasting (C-style)

Here, an integer pointer is used to point to a character, which leads to undefined behavior and risks memory corruption.

Example

In this example, we may lead to unpredictable behavior which unsafely casts a char address to an int and result the output.

#include <iostream>
using namespace std;
int main() {
   char c = 65; // ASCII of 'A'
   int* ptr = (int*)&c; // Unsafe C-style cast
   cout << "Value at int pointer (C-style cast): " << *ptr << endl;
   return 0;
}

Output

The output may vary or crash - undefined behavior:

Value at int pointer (C-style cast): -1165381823

Pointer Typecasting with static_cast (C++)

Here, it shows how static_cast prevents unsafe pointer conversions at compile time, enhancing type safety.

Example

In this program, we cause with a compile-time error as an invalid static_cast from char to int.

#include <iostream>
using namespace std;
int main() {
   char c = 65; // ASCII of 'A'
   // This line will raise a compile-time error
   int* ptr = static_cast<int*>(&c);
   cout << "This line will not compile." << endl;
   return 0;
}

Output

The program above produced the following outcome:

error: invalid static_cast from type 'char*' to type 'int*'

Compile-time Safety

Compile-time Safety means highlighting how C++ catches invalid casts at compile time, whereas C-style casting may allow them to slip through, which can lead to runtime problems.

Example

The program shows that static_cast catches errors at compile-time which performs unsafe casting of a char to an int.

#include <iostream>
using namespace std;
void printInt(int* ptr) {
   cout << "Integer value: " << *ptr << endl;
}
int main() {
   char ch = 'Z';
   // Unsafe C-style cast
   int* p1 = (int*)&ch;
   printInt(p1); // May crash
   // Safe cast will not compile
   // int* p2 = static_cast<int*>(&ch);
   // printInt(p2); // Will be caught at compile-time
   return 0;
}

Output

The output is an integer value: Unpredictable output or crash

Integer value: 20336474
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-04-17T18:01:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements