Integers

An integer is a number without a decimal point – a whole number. Integers are good for problems like counting. Some problems, like counting every person on the planet, require really large numbers. Other problems, like counting the number of children in a classroom, require numbers that aren’t as large.

To address these different problems, integer variables come in different sizes. An integer variable has a certain number of bits in which it can encode a number, and the more bits the variable has, the larger the number it can hold. Typical sizes are: 8-bit, 16-bit, 32-bit, and 64-bit.

Similarly, some problems require negative numbers, while others do not. So, integer types come in signed and unsigned varieties.

An unsigned 8-bit number can hold any integer from 0 to 255. How did I get that? 28 = 256 possible numbers. And we choose to start at 0.

A signed 64-bit number can hold any integer from -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. 263 = 9,223,372,036,854,775,808 minus one bit for the sign (+ or -).

When you declare an integer, you can be very specific:

U​I​n​t​3​2​ ​x​;​ ​/​/​ ​A​n​ ​u​n​s​i​g​n​e​d​ ​3​2​-​b​i​t​ ​i​n​t​e​g​e​r​
S​I​n​t​1​6​ ​y​;​ ​/​/​ ​A​n​ ​s​i​g​n​e​d​ ​1​6​-​b​i​t​ ​i​n​t​e​g​e​r​

However, it is more common for programmers just to use the descriptive types that you learned in Chapter 3.

c​h​a​r​ ​a​;​ ​ ​ ​ ​ ​ ​/​/​ ​8​ ​b​i​t​s​
s​h​o​r​t​ ​b​;​ ​ ​ ​ ​ ​/​/​ ​U​s​u​a​l​l​y​ ​1​6​ ​b​i​t​s​ ​(​d​e​p​e​n​d​i​n​g​ ​o​n​ ​t​h​e​ ​p​l​a​t​f​o​r​m​)​
i​n​t​ ​c​;​ ​ ​ ​ ​ ​ ​ ​/​/​ ​U​s​u​a​l​l​y​ ​3​2​ ​b​i​t​s​ ​(​d​e​p​e​n​d​i​n​g​ ​o​n​ ​t​h​e​ ​p​l​a​t​f​o​r​m​)​
l​o​n​g​ ​d​;​ ​ ​ ​ ​ ​ ​/​/​ ​3​2​ ​o​r​ ​6​4​ ​b​i​t​s​ ​ ​ ​(​d​e​p​e​n​d​i​n​g​ ​o​n​ ​t​h​e​ ​p​l​a​t​f​o​r​m​)​
l​o​n​g​ ​l​o​n​g​ ​e​;​ ​/​/​ ​6​4​ ​b​i​t​s​

Why is char a number? Any character can be described as an 8-bit number, and computers prefer to think in numbers. What about sign? char, short, int, long, and long long are signed by default, but you can prefix them with unsigned to create the unsigned equivalent.

Also, the sizes of integers depend on the platform. (A platform is a combination of an operating system and a particular computer or mobile device.) Some platforms are 32-bit and others are 64-bit. The difference is in the size of the memory address, and we’ll talk more about that in Chapter 8.

Apple has created two integer types that are 32-bit on 32-bit platforms and 64-bit on 64-bit platforms:

N​S​I​n​t​e​g​e​r​ ​g​;​
N​S​U​I​n​t​e​g​e​r​ ​h​;​

In much of Apple’s code, you will see these types used. They are, for all intents and purposes, the same as long and unsigned long.

Tokens for displaying integers

Create a new project: a C Command Line Tool called Numbers. In main.c, create an integer and print it out in base-10 (as a decimal number) using printf():

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​i​n​t​ ​x​ ​=​ ​2​5​5​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​x​ ​i​s​ ​%​d​.​​n​"​,​ ​x​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

You should see something like

x​ ​i​s​ ​2​5​5​.​

As we’ve seen, %d prints an integer as a decimal number. What other tokens work? You can print the integer in base-8 (octal) or base-16 (hexadecimal). Add a couple of lines to the program:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​i​n​t​ ​x​ ​=​ ​2​5​5​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​x​ ​i​s​ ​%​d​.​​n​"​,​ ​x​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​n​ ​o​c​t​a​l​,​ ​x​ ​i​s​ ​%​o​.​​n​"​,​ ​x​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​n​ ​h​e​x​a​d​e​c​i​m​a​l​,​ ​x​ ​i​s​ ​%​x​.​​n​"​,​ ​x​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

When you run it, you should see something like:

x​ ​i​s​ ​2​5​5​.​
I​n​ ​o​c​t​a​l​,​ ​x​ ​i​s​ ​3​7​7​.​
I​n​ ​h​e​x​a​d​e​c​i​m​a​l​,​ ​x​ ​i​s​ ​f​f​.​

(We’ll return to hexadecimal numbers in Chapter 33.)

What if the integer has lots of bits? You slip an l (for long) or an ll (for long long) between the % and the format character. Change your program to use a long instead of an int:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​l​o​n​g​ ​x​ ​=​ ​2​5​5​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​x​ ​i​s​ ​%​l​d​.​​n​"​,​ ​x​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​n​ ​o​c​t​a​l​,​ ​x​ ​i​s​ ​%​l​o​.​​n​"​,​ ​x​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​n​ ​h​e​x​a​d​e​c​i​m​a​l​,​ ​x​ ​i​s​ ​%​l​x​.​​n​"​,​ ​x​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

If you are printing an unsigned decimal number, you should use %u:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​u​n​s​i​g​n​e​d​ ​l​o​n​g​ ​x​ ​=​ ​2​5​5​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​x​ ​i​s​ ​%​l​u​.​​n​"​,​ ​x​)​;​

 ​ ​ ​ ​/​/​ ​O​c​t​a​l​ ​a​n​d​ ​h​e​x​ ​a​l​r​e​a​d​y​ ​a​s​s​u​m​e​d​ ​t​h​e​ ​n​u​m​b​e​r​ ​w​a​s​ ​u​n​s​i​g​n​e​d​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​n​ ​o​c​t​a​l​,​ ​x​ ​i​s​ ​%​l​o​.​​n​"​,​ ​x​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​n​ ​h​e​x​a​d​e​c​i​m​a​l​,​ ​x​ ​i​s​ ​%​l​x​.​​n​"​,​ ​x​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Integer operations

The arithmetic operators +, -, and * work as you would expect. They also have the precedence rules that you would expect: * is evaluated before + or -. In main.c, replace the previous code with a calculation:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​

 ​ ​ ​ ​p​r​i​n​t​f​(​"​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​ ​=​ ​%​d​​n​"​,​ ​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

You should see

3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​ ​=​ ​1​9​

Integer division

Most beginning C programmers are surprised by how integer division works. Try it:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​

 ​ ​ ​ ​p​r​i​n​t​f​(​"​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​ ​=​ ​%​d​​n​"​,​ ​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​1​1​ ​/​ ​3​ ​=​ ​%​d​​n​"​,​ ​1​1​ ​/​ ​3​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

You’ll get 11 / 3 = 3.666667, right? Nope. You get 11 / 3 is 3. When you divide one integer by another, you always get a third integer. The system rounds off toward zero. (So, -11 / 3 is -3)

This actually makes sense if you think 11 divided by 3 is 3 with a remainder of 2. And it turns out that the remainder is often quite valuable. The modulus operator (%) is like /, but it returns the remainder instead of the quotient:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​

 ​ ​ ​ ​p​r​i​n​t​f​(​"​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​ ​=​ ​%​d​​n​"​,​ ​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​1​1​ ​/​ ​3​ ​=​ ​%​d​ ​r​e​m​a​i​n​d​e​r​ ​o​f​ ​%​d​ ​​n​"​,​ ​1​1​ ​/​ ​3​,​ ​1​1​ ​%​ ​3​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

What if you want to get 3.666667? You convert the int to a float using the cast operator. The cast operator is the type that you want placed in parentheses to the left of the variable you want converted. Cast your denominator as a float before you do the division:

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​

 ​ ​ ​ ​p​r​i​n​t​f​(​"​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​ ​=​ ​%​d​​n​"​,​ ​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​1​1​ ​/​ ​3​ ​=​ ​%​d​ ​r​e​m​a​i​n​d​e​r​ ​o​f​ ​%​d​ ​​n​"​,​ ​1​1​ ​/​ ​3​,​ ​1​1​ ​%​ ​3​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​1​1​ ​/​ ​3​.​0​ ​=​ ​%​f​​n​"​,​ ​1​1​ ​/​ ​(​f​l​o​a​t​)​3​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Now, floating point division will be done instead of integer division, and you’ll get 3.666667. Here’s the rule for integer vs. floating-point division: / is integer division only if both the numerator and denominator are integer types. If either is a floating-point number, floating-point division is done instead.

Operator shorthand

All the operators that you’ve seen so far yield a new result. So, for example, to increase x by 1, you would use the + operator and then assign the result back into x:

i​n​t​ ​x​ ​=​ ​5​;​
x​ ​=​ ​x​ ​+​ ​1​;​ ​/​/​ ​x​ ​i​s​ ​n​o​w​ ​6​

C programmers do these sorts of operations so often that operators were created that change the value of the variable without an assignment. For example, you can increase the value held in x by 1 with the increment operator (++):

i​n​t​ ​x​ ​=​ ​5​;​
x​+​+​;​ ​/​/​ ​x​ ​i​s​ ​n​o​w​ ​6​

There is also a decrement operator (--) that decreases the value by 1:

i​n​t​ ​x​ ​=​ ​5​;​
x​-​-​;​ ​/​/​ ​x​ ​i​s​ ​n​o​w​ ​4​

What if you want to increase x by 5 instead of just 1? You could use addition and assignment:

i​n​t​ ​x​ ​=​ ​5​;​
x​ ​=​ ​x​ ​+​ ​5​;​ ​/​/​ ​x​ ​i​s​ ​1​0​

But there is a shorthand for this, too:

i​n​t​ ​x​ ​=​ ​5​;​
x​ ​+​=​ ​5​;​ ​/​/​ ​x​ ​i​s​ ​1​0​

You can think of the second line as assign x the value of x + 5. In addition to +=, there is also -=, *=, /=, and %=.

To get the absolute value of an int, you use a function instead of an operator. The function is abs(). If you want the absolute value of a long, use labs(). Both functions are declared in stdlib.h:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​ ​=​ ​%​d​​n​"​,​ ​3​ ​*​ ​3​ ​+​ ​5​ ​*​ ​2​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​1​1​ ​/​ ​3​ ​=​ ​%​d​ ​r​e​m​a​i​n​d​e​r​ ​o​f​ ​%​d​ ​​n​"​,​ ​1​1​ ​/​ ​3​,​ ​1​1​ ​%​ ​3​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​1​1​ ​/​ ​3​.​0​ ​=​ ​%​f​​n​"​,​ ​1​1​ ​/​ ​(​f​l​o​a​t​)​3​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​a​b​s​o​l​u​t​e​ ​v​a​l​u​e​ ​o​f​ ​-​5​ ​i​s​ ​%​d​​n​"​,​ ​a​b​s​(​-​5​)​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset