C++ constants, escape codes and strings

In this tutorial we will take a look at constants and escape codes. We will also introduce strings.

Constants

The difference between variables and constants is that variables can change their value at any time but constants can never change their value. (The constants value is locked for the duration of the program.)
Constants can be very useful, PI for instance is a good example to declare as a constant.

Defining a constants

With the pre-processor directive “define” it is possible to define your own constants.

The format of the pre-processor directive #define is:
#define identifier value

Take a look at an example:


        #define PI 3.14

	#define A 5

Escape codes

Escape codes are used to represent characters that are difficult to express otherwise in the source code.
For instance a tab (\t). Escape codes all start with a backslash (\).

\n

Newline

\r

carriage
return

\t

Tab

\v

vertical
tab

\b

Backspace

\f

form feed
(page feed)

\a

alert
(beep)

\’ single quote ()
\” double quote ()
\? question mark (?)
\\ backslash (\)

Escape codes can be used like so:

	
        "jane\n"

	"jane\t doe"

	etc.

Escape codes can also be used to express octal (base-8) or hexadecimal (base-16) numbers. An octal number can be used like this: \10 (backslash followed by a number.)

A hexadecimal number can be used like this: \xF0 (a backslash followed by an x and the number.)

It is also possible to define an escape code.

For example:



	#include<iostream>

	using namespace std;

	#define PI 3.14159	// note (1)

	#define NEW_LINE '\n'; 	// note (2)

	int main ()

	{

		float my_var=5.0;

		my_var = my_var * 2; 

		cout << my_var;

		cout << NEW_LINE; 

		return 0;

	}

Note: (1) The #define directive is not a C++ statement but a directive for the pre-processor. It assumes the entire line is the directive and does not require a semicolon.

(2) If a semicolon character ( ; ) is appended at the end of the define statement, the pre-processor will replace all NEW_LINE for \n.

const

The const prefix can be used to declare a constant of a specific type.
(The same way as you would declare any other variable.)

Take a look at the example:

	
        const int x = 50;

	const char newline = '\n';

	const y = 50;

Note: If no data type is presented (last example) the compiler will assume a data type!
In this case it will assume that the data type is an integer (int).

Introduction to strings

In this paragraph we take a brief look at strings. In a later tutorial we will take a closer look at strings.
Variables that can store more then a single character are known as strings.

The C++ Standard Template Library (STL) contains a string class that has to be included before you can make use of strings. Also you need to have access to the standard namespace.

Take a look at an example:



	#include<iostream>

	#include<string>   //add for std::string

	using namespace std;

	int main()

	{

		string mystring = "This is a string";

		cout << mystring;

		return 0;

	}

Note: Instead of “string mystring” you could also say “std::string mystring” if you don’t want to include using namespace std. Instead of mystring = “This is a string”; it is also possible to make use of parentheses: mystring = (“This is a string”);

Strings can also perform all the other basic operations that fundamental data types can. For instance, being declared without an initial value and being assigned values during execution.

Take a look at the following example:



	#include<iostream>

	#include<string>

	using namespace std;

	int main ()

	{

		string mystring;

		mystring = "First value";

		cout << mystring << endl;

		mystring = "Second value";

		cout << mystring << endl;

		return 0;

	}

That’s all for this tutorial. In the next tutorial we will look at operators.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.