Course contentsShow
← C
Lesson 81 of 1,2192. Variables and TypesFree lesson

The `int` keyword

The int keyword

What you'll learn: You will learn how to use the int keyword to store whole numbers in your computer's memory.

Your computer’s "Whole Number" box

In C, if you want to store a number like 5, -42, or 1,000, you use the int type. The name is short for "integer," which is just a fancy math word for a whole number. Think of an int as a specific size of cardboard box in your computer's memory. This box is perfectly shaped to hold whole numbers, but it isn't designed for anything else—it can't hold fractions, decimals, or letters.

Declaring and assigning

To use an int, you first have to tell C that the box exists. This is called "declaring" a variable. You give the box a name so you can find it later.

int playerHealth;
playerHealth = 100;

int applesInBasket = 5;

In the example above, int playerHealth; tells the computer: "Reserve a spot in memory for a whole number and call it playerHealth." You can then put a value in it using the equals sign. You can also do both steps at once, as shown with applesInBasket.

The limits of the box

Every box has a physical limit. While a standard int is usually large enough for daily tasks—typically holding numbers up to about 2 billion—it cannot store decimals. If you try to shove 5.9 into an int box, C won't break; it will simply chop off the .9 and store the 5. It doesn't round up; it just discards whatever doesn't fit the "whole number" rule.

Because int is efficient and fast for the computer to process, it is the "go-to" tool for counting things, like the number of lives remaining in a game or the current year.

Key takeaway

Use int to store whole numbers without decimals, such as 10, 0, or -500.