15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

528<br />

Using Operators<br />

Table 1-2<br />

Typical Floating Point Data Types<br />

Data Type Number of Bytes Range<br />

Float 4 –1.4023 E–45 to 3.4028 E38<br />

Double 8 –4.9406 E– 324 to 1.7977 E308<br />

Long double 8 or 12 –4.9406 E– 324 to 1.7977 E308<br />

Real numbers (float, double, and long double) are always signed data types<br />

(positive or negative). With many compilers, the range of values is identical<br />

between double and long double data types.<br />

Declaring Boolean values<br />

In the C language, there are no Boolean data types. Any nonzero value<br />

is considered to represent True, and zero represents False. To mimic a<br />

Boolean data type, many C programmers define numeric values <strong>for</strong> True<br />

and False, such as<br />

#define FALSE 0<br />

#define TRUE 1<br />

int flag = FALSE;<br />

Although this is workable, such an approach <strong>for</strong>ces you to create an integer<br />

variable to represent a Boolean value. To avoid this problem, C++ offers a<br />

special Boolean data type (like most <strong>programming</strong> languages), such as<br />

bool variablename;<br />

A C++ Boolean data type can hold a value of 0 (False) or 1 (True).<br />

Using Operators<br />

The three types of operators used are mathematical, relational, and logical.<br />

Mathematical operators calculate numeric results such as adding, multiplying,<br />

or dividing numbers, as shown in Table 1-3.<br />

Table 1-3<br />

Mathematical Operators<br />

Mathematical Operator Purpose Example<br />

+ Addition 5 + 3.4<br />

- Subtraction 203.9 – 9.12<br />

* Multiplication 39 * 146.7<br />

/ Division 45/ 8.41<br />

% Modula division (returns the remainder) 35 % 9 = 8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!