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.

526<br />

Declaring Variables<br />

Declaring string data types<br />

Unlike other <strong>programming</strong> languages, C/C++ doesn’t support a string data<br />

type. Instead, C/C++ offers two alternatives — a character data type and a<br />

library.<br />

First, C/C++ offers a character data type, which can hold a single character.<br />

Instead of using a string data type, you can use an array of character data<br />

types, which is clumsier, but workable. To declare a character data type, use<br />

the char keyword followed by your variable name, such as<br />

char variablename;<br />

To mimic a string data type, declare an array of characters, such as<br />

char arrayname[arraylength];<br />

So if you wanted to create an array that can hold 20 characters, you could<br />

do this:<br />

char firstname[20];<br />

A second alternative is to include a library (string) that implements a string<br />

data type, such as<br />

#include <br />

using namespace std;<br />

string stringvariable;<br />

Declaring integer data types<br />

Whole numbers represent integers, such as –59, 692, or 7. A whole number<br />

can be positive or negative. The most common type of integer data type is<br />

int and is used as follows:<br />

int variablename;<br />

If you need to restrict a variable to a smaller range of values, you can declare<br />

a short int or a short variable as follows:<br />

short int smallvariable;<br />

short smallvariable;<br />

The long integer data type is often identical to the regular int data type.

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

Saved successfully!

Ooh no, something went wrong!