C Programming Tutorial 21 – Int, Float, and Double Data Types

Don’t forget to read the last blog on data types! Are you new here? Start at the beginning, Intro to C!

Now, we’ve discussed data types some, but this video is going to be devoted to discussing numeric data types. Numeric data types are used to describe numbers. So any number in our program can be classified as one of many numeric data types.

There are a ton of numeric data types, but most are just modified versions of the three main numeric data types: integer, float, and double.

int stands for integer. Integers are whole numbers, that is, any number with no fractional part. Integers = {…, -2, -1, 0, 1, 2, …}.

Floats and doubles are used to store numbers with numbers after the decimal point. So think .5, or .2342, or 1.2, or 430.0. These two data types collectively are called floating point data types. That can be kind of confusing because one is called float, but they are actually both called floating point. The float data type is called single precision floating point, while double is called double precision floating point.

So let’s say you wanted to store how many dogs you owned… Are you going to want to use int, float or double? Probably int, because you should not have a fraction of a dog for a pet. That’s just messed up.

So if there are two data types for storing numbers with fractional parts, how are you supposed to know which one you should use? It all has to do with precision.

Essentially, when you declare a variable of type double, it takes up twice as much storage as float, but it allows for around twice as much precision. So if you want to store something like the square root of two, using a float will give you only so many digits, and using double will give about double as many digits.

In general, I prefer to use doubles for about everything.

So we are going to practice creating some variables and outputting them to the screen.

To start, we will output an integer. This is super easy. You can use either %d or %i. They both work exactly the same. You think of %i being short for integer, if that helps you.

You can see that declaring and outputting an integer is a piece of cake.

Now, when we output a floating point number, we need to use %f. Let’s give it a try.

Super easy.

Now, there is something important you need to know about the float and double data type. Due to the way these data types are stored, they cannot represent every value perfectly. For example:

When we run this, the output may not be what you expect.

This has to do with the fact that certain numbers are impossible to represent with a finite number of binary digits. If you have no idea what I’m talking about…just trust me for now. We can talk about it some other day, ha!

I do think we should talk a little bit about how numbers are stored, though. They are actually stored in scientific notation. We are going to discuss how this works in the next blog! Let’s go!

Leave a Reply

Your email address will not be published. Required fields are marked *