TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
(type) operator, formally known as the explicit type cast operator, is a unary operator that instructs the compiler to convert the evaluated result of an expression into a specified scalar data type or void.
Mechanics and Evaluation
- Rvalue Generation: The cast operator yields a new value (an rvalue) of the specified
type_name. It does not modify the type or the stored value of the original operand in memory. - Precedence and Associativity: It is a Level 2 operator in C precedence (alongside other unary operators like
&,*,sizeof), evaluating right-to-left. Because of its high precedence, the cast applies to the immediate adjacent operand. Casting the result of an entire operation requires enclosing the expression in parentheses:(type_name)(expression). - Compile-Time Resolution: The conversion logic is determined at compile time. Depending on the source and target types, the compiler emits instructions to perform bitwise truncation, sign/zero extension, floating-point conversion, or pointer type adjustment.
Type Constraints
The C Standard imposes strict rules on what types are eligible for casting:- Scalar to Scalar: If the target
type_nameis a scalar type (arithmetic or pointer), the original expression must also evaluate to a scalar type. - Non-Scalar Types: You cannot cast to an aggregate type (arrays or
structs) or auniontype. You also cannot cast from an aggregate oruniontype, with the sole exception of casting tovoid. - Void: Any expression can be cast to
void. This explicitly indicates to the compiler that the evaluated result of the expression is intentionally discarded.
Type Qualifiers
The cast operator interacts directly with type qualifiers (const, volatile, restrict). A primary mechanic of explicit pointer casting is adding or removing these qualifiers from a type. While casting away a qualifier (e.g., converting const int * to int *) is syntactically valid and yields an unqualified pointer, attempting to modify an originally const-qualified object through that new pointer results in undefined behavior.
Conversion Behaviors
When the(type) operator is applied, the underlying bit-level transformation depends on the type categories involved:
- Integer to Integer:
- Downcasting (Larger to Smaller): If the target type is unsigned, the value is truncated by discarding the higher-order bits. If the target type is signed and the value cannot be represented in the new type, the result is implementation-defined or an implementation-defined signal is raised.
- Upcasting (Smaller to Larger): The value is extended. Unsigned types undergo zero-extension; signed types undergo sign-extension.
- Floating-Point to Integer: The fractional portion is truncated (rounded towards zero). If the integral part cannot be represented by the target integer type, the behavior is undefined.
- Integer to Floating-Point: If the integer value cannot be represented exactly in the target floating-point type, the result is either the nearest higher or nearest lower representable value. The exact choice is implementation-defined or dictated by the active floating-point rounding mode.
- Pointer to Pointer: The cast operator changes the type of the pointer expression itself. The conversion is undefined behavior if the resulting pointer is not correctly aligned for the referenced type. If alignment requirements are met, the memory address remains unchanged, but subsequent dereferencing of the new pointer is subject to strict aliasing rules.
- Pointer to Integer / Integer to Pointer: The conversion is implementation-defined. The integer type must be sufficiently large (e.g.,
uintptr_t) to hold the pointer value without truncation, otherwise undefined behavior occurs.
Syntax Visualization
Master C with Deep Grasping Methodology!Learn More





