O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Casting

I have previously mentioned that data types and variables are rather like pegs and holes, and that it is sometimes possible to fit a square peg into a small round hole, losing some information, or fitting it into a large round hole, with some extra space. The programmer can make these decisions when writing the code, and this is called casting.

In C, a mechanism is provided by which a programmer can explicitly or implicitly cast a variable from one type to another. Casting is both useful and dangerous; it is strongly advised to only use explicit casting, and even that in recognized situations only. It is far better practice to select the correct type in the first place.

Generally speaking, casting is a number-to-number operation and can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required