O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

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

Variable Conversion

Over the course of this chapter you've seen many ways of working with the various number types. One last technique to be addressed is that of variable conversion: changing the value of a variable from one type (say, float) to another (integer).

C will automatically convert values to match types; for example:

int a;
float b = 3.14;
a = b; /* a is 3 */
b = a; /* b is 3.0 */

In simplest terms, this means that the decimal value is dropped when converting from a float to an integer and that a 0 is added when going the other way.

You can also manually convert a value from one type to another, a process called type casting. To do so, precede the value being converted with the new type in parentheses. For example:

 int a; float ...

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