Identifiers, reserved words, literals, escape sequences

Identifiers, reserved words, literals, escape sequences



1. What appointment of identifiers in C++? The rules of creating the identifiers.

The identifier is the same name, that is assignment by user for variable, constant, function, own type or other object. Identifiers can consist of one or more characters. Variable names must begin from a letter or underscore. In C++, uppercase and lowercase characters are treated as different characters (unlike Pascal and other languages).

Examples of identifiers:

x
max
Delta
my_method
w23x5

2. What elements of C ++ cannot be used as an identifier?

In C++ cannot be used as identifiers the keywords.

Keyword list in C ++:

asm           auto           bool              break
case          catch          char              class
const         const_class    continue          default
delete        do             double            dinamic_cast
else          enum           explicit          export
extern        false          float             for
friend        goto           if                inline
int           long           mutable           namespace
new           operator       private           protected
public        register       reinterpret_cast  return
short         signed         sizeof            static
static_cast   struct         switch            template
this          throw          true              try
typedef       typeid         typename          union
unsigned      using          virtual           void
volatile      wchar_t        while

3. What are the literals in C++?

Literals – it is the fixed values (constants), that cannot be changed by program when executing. Literals (constants) can be any of base types. The way of representation each constant depends on it’s type.

Examples of literals:

23
-8.45
's'
L'A'
12.345F
4.3e-8
4.3E-8
1300L

4. In what way the compiler recognizes the integer literals?

The C++ compiler tries independently to define the type of literal by it’s value.

For example, in 16-bit environment:

  • number 23 is linked with int type;
  • number 300929 is linked with long int type.

If you need to set the value of long int type compulsorily, then you need to add the suffix ‘L‘ or ‘l‘. For example:

20L
-300l

If you need to set the value of literal as unsigned (by using prefix ‘unsigned‘), then you need add suffix ‘U‘ or ‘u‘. For example:

1500u  // the "unsigned long int" type
1500U  // unsigned int
1500u  // unsigned int
1500ul // unsigned long int
1500UL // unsigned long int

5. In what case the compiler recognizes the floating point literals?

By default, all floating point constants are the double type. If you need set the “float” type, then you need add the suffix ‘f‘ of ‘F’. If you need set the “long double” type, then it is needed add suffix ‘L’ of ‘l’. For example:

9000.0009f        // float
8888.777F         // float
111.222           // double
0.33L             // long double
-56785757574.33l  // long double

6. How are presented the hexadecimal and octal literals?

Sometimes it is expedient to use hexadecimal or octal notation instead the decimal notation. Literals in hexadecimal or octal notation are used only for integers.

In the octal notation are used the numbers from ‘0‘ to ‘7‘. In the hexadecimal notation are used the numbers from ‘0‘ to ‘9‘ and letters from ‘A‘ to ‘F‘.

Octal literals are begun from the prefix ‘0‘.

Hexadecimal literals are begun from the prefix ‘0x‘ or ‘0X‘. For example:

010    // Number 10 in the octal notation
055    // octal notation
0x10   // hexadecimal notation
0XAFD  // hexadecimal notation

7. What is the purpose of escape sequences?

Programming languages include the several constants, that you cannot type from keyboard in the program source. For example, the carriage return character.

Moreover, some symbols, such as single or double quotes, have the special assignment. Therefore, you cannot type their directly.

To avoid this problem, in the C++ (and other languages) are entered the escape sequences. These sequences replace the characters, that have the special appointment or characters, that you cannot type from keyboard directly.

The escape sequences in C++:

\b - backspace
\f - move the cursor to the beginning of next page
\n - new line
\r - carriage return
\t - horizontal tabulation
\" - double quote
\' - single quote
\\ - backslash
\v - vertical tabulation
\a - sound signal
\? - question mark
\N - octal constant
\x - hexadecimal constant

Example of definition the variable, that takes the symbol of escape sequence.

char c1, c2;
c1 = '\\';   // backslash
c2 = '\"';   // double quote
c1 = '\23';  // the number 23 in the octal numeral system
c2 = '\x5F'; // the number 5F in the hexadecimal numeral system