Previous | Table of Contents | Next |
Table 2.1 Characteristics of common data types. | ||||
Type | Kind of data | Typical size | printf format symbol: | scanf format symbol: |
---|---|---|---|---|
short | integer | 2 bytes | %d | %d |
long | integer | 4 bytes | %d | %ld |
int | integer | 2 or 4 bytes | %d | %d |
float | floating point | 4 bytes | %f | %f |
double | floating point | 8 bytes | %f | %lf |
![]() | %ld and %lf are discussed under Getting Input later in this chapter. |
The size of a data type determines its range. For example, the range of a short is -32,768 to 32,767. The range of a long is approximately plus or minus two billion.
For a more comprehensive list of data types and their ranges, see the topic Data Types in Part III.
If youre like me, you dont like to place gobbledygook into your program without knowing what its all about. I can hear you saying, Why do I have to enter this #include thing? Shouldnt I just be able to tell C++ Print the darn string!?
I sympathize, particularly if youre coming from Basic and all youve had to do is to say PRINT X. However, the #include directive serves a very good purpose.
In C++, every function (except main) must be declared before being used. This goes for standard library functions, such as printf. Rather than declare printf yourself, it is easier to include a special file for this purpose (called a header file) that includes all the declarations you need. For example, the file STDIO.H has definitions for all the standard input/output functions, such as printf.
There are a couple of catches with #include. First, do not place a semicolon at the end. This rule seems a little arbitrary, although there are technical reasons for it. In any case, just commit to memory this Golden Rule of Directives: If a keyword begins with a pound sign (#), it is a directive, which means that you dont end the line with a semicolon (;). The second catch is that you have to know which header file to refer to. The solution is easy: when you look up the function in standard-library documentation, the first thing it tells you is which header file to use.
You never include the same header file more than once. If you call 10 functions but they are all declared in STDIO.H, for example, you need only one #include directive.
Once youre familiar with the C++ standard library, you can usually figure out which header file(s) you need without having to look up anything. The most common library functions fall into one of the following general categories shown in Table 2.2.
Table 2.2 Common C++ include Files. | ||
Header file | Include directive | Includes declarations for these functions |
---|---|---|
STDIO.H | #include <stdio.h> | Standard input and output functions, including functions that perform file operations. |
IOSTREAM.H | #include <iostream.h> | Stream operators (C++ only), which can be used to replace printf and scanf. Chapter 4 explains the use of these. |
STRING.H | #include lgstring.hgt | String-manipulation functions; for example, copy one string to another. |
CTYPE.H | #include <ctype.h> | Functions for testing and changing the type of individual characters in a string. |
MATH.H | #include lsmath.hgt | Trig, logarithmic, exponential, and other fun things that engineers love to play with. |
MALLOC.H | #include lsmalloc.hgt | Functions for dynamically getting and freeing blocks of memory from the operating system. |
The following syntax display summarizes the general pattern for a simple C program. It doesnt involve any functions except main, but later in the chapter later, well add functions to the syntax. Note that all the #include directives should come before anything else in the program.
include_directives void main () { data_declarations_and_other_statements }
The placeholder data_declarations_and_other_statements includes both data declarations, which you were introduced to in the previous section, and other statements. (These other statements are often called executable statements. In C++, however, data declarations can also involve executable code.) This last category is a large one, and I break it down somewhat in the next section.
![]() | With C, all data declarations must appear before all other statements. C++ relaxes this restriction, letting you declare a variable any time (and almost anywhere) you want to. |
After declaring variables, you can use statements to perform a number of actions. These actions usually manipulate or use the variables in some way. In C++, you can mix declarations and other statements in any order. (You cant do that in C.) But it usually makes sense to declare variables first, because in both C and C++ you must explicitly declare a variable before using it.
Aside from control structures and functions, which Ill cover later, all the actions you can perform boil down to these three:
The next three sections discuss how you do each of these actions in C++.
Assignment statements in C++ look similar to those in other languages, especially Basic. The main difference from Basic is that C++ statements are terminated by semicolons.
Before assigning data to a variable, make sure you declare the variable:
int amount, a, b, c;
To assign a value to one of these variables, place the variable name on the left side of an equal sign (=). On the right side, place a variable, a constant (such as 1 or -240), or a compound expression (such as 2 * c).
a = 1; amount = -240; b = 2 * c; amount = a + 10 * b * -1;
Note that in C++, the asterisk (*) represents multiplication.
Previous | Table of Contents | Next |