Sams Teach Yourself C in 21 Days

(singke) #1
This listing is pretty straightforward. A number is entered by the user. The pro-
gram uses the C++ input object,cin, to retrieve this number. The cinobject does
just the opposite of the coutobject you learned a little bit about earlier. You can redirect
input into a variable from the cinobject. In this case, you are directing a value into the
nbrvariable. After it is obtained, the value is squared, halved, and then the squared value
is halved. Each of these values is printed out.
An inline function is declared and defined in line 4 of the listing, as well as in line 9.
Other than the use of the keyword,inline, inline functions are declared just as any other
function is declared. As stated before, the inlinespecifier is a request to the compiler to
place the code of these functions inline. If the request is honored, the code is duplicated.
Listing B2.7 shows roughly what the resulting code looks like if the inline request is
honored.

660 Bonus Day 2

ANALYSIS

Listing B2.7 shows how inline functions affect the code. The compiler also
expands out the includes, drops the comments, compresses white space, and
more.

Note


LISTINGB2.7 Resulting code if inline request is honored
1: //Using inline functions
2: #include <iostream.h>
3:
4: int main(int argc, char* argv[])
5: {
6: long nbr;
7:
8: cout <<”\nEnter a number: “;
9: cin >> nbr;
10:
11: cout <<”\n\nSquared: “ << (nbr * nbr);
12: cout <<”\nHalved: “ << (nbr/2);
13:
14: cout <<”\nHalf the square: “;
15: cout << ((nbr * nbr)/2);
16:
17: cout << “\n\nDone!”;
18:
19: return 0;
20: }

37 448201x-Bonus2 8/13/02 11:18 AM Page 660

Free download pdf