A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../../cpp/../c/language/cast.html below:

cast operator - cppreference.com

Performs explicit type conversion

[edit] Syntax

where

[edit] Explanation

If type-name is void, then expression is evaluated for its side-effects and its returned value is discarded, same as when expression is used on its own, as an expression statement.

Otherwise, if type-name is exactly the type of expression, nothing is done (except that if expression has floating type and is represented with greater range and precision than its type indicates – see below).

Otherwise, the value of expression is converted to the type named by type-name, as follows:

Every implicit conversion as if by assignment is allowed.

In addition to the implicit conversions, the following conversions are allowed:

In any case (both when executing an implicit conversion and in the same-type cast), if expression and type-name are floating types and expression is represented with greater range and precision than its type indicates (see FLT_EVAL_METHOD), the range and precision are stripped off to match the target type.

The value category of the cast expression is always non-lvalue.

[edit] Notes

Because const, volatile, restrict, and _Atomic qualifiers have effect on lvalues only, a cast to a cvr-qualified or atomic type is exactly equivalent to the cast to the corresponding unqualified type.

The cast to void is sometimes useful to silence compiler warnings about unused results.

The conversions not listed here are not allowed. In particular,

If the implementation provides intptr_t and/or uintptr_t, then a cast from a pointer to an object type (including cv void) to these types is always well-defined. However, this is not guaranteed for a function pointer.

(since C99)

Note that conversions between function pointers and object pointers are accepted as extensions by many compilers, and expected by some usages of POSIX dlsym function.

[edit] Example
#include <stdio.h>
 
int main(void)
{
    // examining object representation is a legitimate use of cast
    const double d = 3.14;
    printf("The double %.2f (%a) is: ", d, d);
    for (size_t n = 0; n != sizeof d; ++n)
        printf("%02X ", ((unsigned char*)&d)[n]);
 
    // edge cases
    struct S { int x; } s;
    // (struct S)s; // error; not a scalar type, even though
                    // casting to the same type does nothing
    (void)s; // okay to cast any type to void
}

Output:

The double 3.14 (0x1.91eb851eb851fp+1) is: 1F 85 EB 51 B8 1E 09 40
[edit] References
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4