C Reference ยท Types and Literals
Casts in C
Reference for C casts: the (type)expr syntax, precedence, numeric and pointer casts, casting away const, casting to void, and when a cast hides a bug.
A cast is an explicit request to convert a value to another type. It is written as a type name in parentheses in front of an expression: (double)total. The result is a value (never an lvalue) of the new type. A cast does not change the original object.
C has a single cast syntax for every kind of conversion, from harmless numeric widening to risky pointer reinterpretation. That makes casts easy to write and easy to overuse. Each cast tells the compiler "I know better", so it also silences warnings that might have caught a real mistake.
double average = (double)total / count; /* convert total first, then divide */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
( type-name ) cast-expression
| Part | Description |
|---|---|
type-name |
Any complete type, including pointer types such as (char *) and (void). |
cast-expression |
The value to convert. The cast has unary precedence, so it binds tighter than *, / and +. |
Result: the value converted to type-name, with its qualifiers dropped (a cast to const int yields a plain int value).
Errors / undefined behavior: a cast to a type with a wrong alignment, a floating value that does not fit the target integer, and reading an object through a pointer of an incompatible type are all undefined behavior. A cast to a struct, union or array type is a constraint violation (a compile error), and a cast to void is the only cast that discards a value.
Usage notes
- Precedence matters.
(double)a / bconvertsafirst.(double)(a / b)divides integers and then converts, which is usually not what you want. - Numeric casts follow the normal conversion rules: float to int truncates toward zero, and converting to an unsigned type wraps modulo 2^N. See type conversions.
- Converting a value to a signed type that cannot hold it gives an implementation-defined result (GCC wraps) or raises a signal.
- A cast to
voidexplicitly discards a value. It silences "unused result" warnings and documents intent. - Pointer casts do not convert the data, they only reinterpret the address. Reading the object through an incompatible type breaks the strict aliasing rule. The character types (
char,unsigned char,signed char) are the exception: you may examine any object's bytes that way. - Never cast away
constto modify the object. Castingconst char *tochar *is legal, but writing to an object that was definedconstis undefined behavior. mallocneeds no cast in C.void *converts implicitly. A cast adds noise and can hide a missing#include <stdlib.h>on old compilers (in C++ it is required).- Casting function pointers to another signature and calling through it is undefined behavior. Converting back to the original type is fine.
- Pointer to integer casts are implementation-defined. Use
uintptr_twhen you must (see intptr_t and uintptr_t). - Prefer no cast. Every cast is a spot where the compiler stopped checking. Turn on
-Wconversionto find the ones that matter.