Last Updated:

C++ Data Types and Alphabet

In this lesson, you will learn the alphabet of the C++ language, as well as what types of data a program can process in it. This may not be the most exciting moment, but this knowledge is necessary!In addition, starting to learn any other programming language, you will go through a similar stage of training with greater confidence. A C++ program can contain the following characters:

  • uppercase, lowercase Latin letters A, B, C..., x, y, z and an underscore;
  • Arabic numerals from 0 to 9;
  • special characters: { } , | , [ ] ( ) + — / % * . \ ‘ : ? < > = ! & # ~ ; ^
  • Space, tab, and line break characters.

You can use comments in the program test. If text with two slashes // ends with a line break or is enclosed between /* and */, the compiler ignores it.

Data in C++

To solve the problem, any data is processed in any program. They can be of various types: integers and real numbers, symbols, strings, arrays. It is customary to describe data in the C++ language at the beginning of a function. The main types of language data include:

  • char — symbolic;
  • int — integer;
  • float — floating point;
  • double — double precision;
  • bool — logical;

To form other types of data, basic and so-called specifiers are used. There are four defined data type specifiers in C++:

  • short — short;
  • long — long;
  • signed — symbolic;
  • unsigned — unsigned.

When you need to initialize a constant variable (the value remains unchanged), you should write const before the type. For example:

const int var;

Character type

Char data in a computer's memory always takes up 1 byte. A character type can be with or without a sign. Signed char values can store values from -128 to 127. Accordingly, the values of variables of type unsigned char can be in the range from 0 to 255. When working with character data, keep in mind that if a single character occurs in an expression, it must be enclosed in single quotation marks ('a').

Integer type

A variable of type int in the computer's memory can occupy either 2 or 4 bytes. It depends on the bit depth of the processor. By default, all integer types are considered signed, which means that the signed specifier can be omitted. The unsigned specifier allows you to represent only positive numbers. the following are some ranges of integer type values

TypeRangeSize
int-2147483648... 21474836474 bytes
unsigned int0... 42949672954 bytes
signed int-2147483648... 21474836474 bytes
short int-32768... 327672 bytes
long int-2147483648... 21474836474 bytes
unsigned short int0... 655352 bytes

 

Material type

A floating-point number is represented in the form mE +- p, where m is a mantissa (an integer or fractional number with a decimal point), p is an order (an integer). Typically, float values occupy 4 bytes, and double 8 bytes. Table of ranges of values of real type:

float3,4E-38... 3,4E+384 bytes
double1,7E-308... 1,7E+3088 bytes
long double3,4E-4932... 3,4E+49328 bytes

 

Boolean type

bool variable can only accept two values of true or fasleAny value that is not zero is interpreted as true. False is represented in memory as 0.

Type void

Many values of this type are empty. It is used to define functions that do not return values, to specify an empty list of function arguments, how the base type for pointers is, and in type conversion operations.

Data Type Conversion

In C++, there are two types of data type conversion: explicit and implicit.

  • Implicit conversion occurs automatically. This is done during the comparison, assignment, or evaluation of expressions of different types. For example, the following program displays a float value on the console.

1

2

3

4

5

6

7

8

9

10

#include "stdafx.h"

#include <iostream>

using namespace std;

int main()

{

int i=5; float f=10.12;

cout<<i/f;

system("pause>>void");

return 0;

}

The highest priority is the type in which information is lost the least. Do not abuse implicit type conversion, as all sorts of unforeseen situations can arise.

  • Explicit transformation, unlike implicit conversion, is carried out by the programmer. There are several ways to do this:
  1. Convert to C(float) a styles
  2. Converting to C++ Styles: float()

Also, type conversion can be carried out using the following operations:

1

2

3

4

static_cast <> ()

const_cast <> ()

reinterpret_cast <> ()

dynamic_cast <> ()

static_cas - Performs the conversion of related data types. This statement quotes types according to the usual rules, which may be necessary in the case when the compiler does not perform automatic conversion. The syntax will look like this:

Type static_cast <Type> (object);

With static_cast, you can't remove the constancy of a variable, but the next operator can do it. const_cast is used only when you want to remove the constancy of an object. The syntax will look like this:

Type const_cast <Type> (object);

reinterpret_cast is used to convert pointers of different types, integers to pointers and vice versa. If you see a new word "pointer" – do not be alarmed! this is also a type of data, but we will not work with it soon. The syntax here is the same as that of the previously discussed operators:

Type reinterpret_cast <Type> (object);

dynamic_cast is used for dynamic type conversion that implements the casting of pointers or references. Syntax:

Type dynamic_cast <Type> (object);

Control characters

You are already familiar with some of these "control characters" (for example, \n)They all start with the reverse "slash" and are also framed by double quotation marks.

Image

Hexadecimal code

Name

\a

7

Beeper beep

\b

8

Step back

\f

With

Page translation (format)

\n

And

Line feed

\r

D

Carriage return

\t

9

Horizontal tabs

\v

In

Vertical tabs

\\

5C

Backslash

\’

27

Apostrophe

22

Quotation mark

\?

3F

Question mark

\0ddd

Octal symbol code

\0xddd

ddd

Hexadecimal character code

Control characters, which by the way can be found under the names "escape-sequences", are mainly used to display symbols that do not have a visual effect.