Conversion types

type conversion c ++, for beginners, for Dummies, from scratch, основы программирования c  Type conversion in C ++ rather old, or rather a classic operation. In short and in Russian: Conversion type – it is considered an indication of a set of computer bytes integer or real. Or a pointer to a specific class.

At all, to understand the type conversion is necessary to consider the mechanism and structure variables. For example, PAC line integer multiple: short int, int, long int, not counting the unsigned. And computer places for them a different number of memory cells (bytes): 1, 2 and 4 respectively. Convert within these types of one class is called the standard conversion. at:

Take the next: take the two younger byte of a variable "a", simply transplanted in two bytes of variable «b». Since the value in and (unit) it is currently placed in these two bytes, the program will give the correct result, and C ++ will not be against such a conversion.

Why do I need to consider the size of the variable in the conversion?

What happens if you put in a variable "a" number 123456789? It is perfectly fit in 4 byte type int, but 2 byte type short int it no longer falls. too much. С , as expected, it just cuts the long number, taking only the tail of it, and put into a variable «b». We obtain course nonsense. And with a minus, if you bring it to the screen. Converting from the variable, which occupies a greater number of bytes in the memory will not be correct. If you convert the contrary from the short in long for example such problems will not, because the bytes in the variable recipient longer, and places data that stores short quite enough.

It is necessary to know. Teachers often catch students on this focus, especially in the training manual giving a similar pitfall with variable poorly compatible with each other in size.

As for the types of real conversion, It is engaged in a math coprocessor. There is no longer just the bytes distilled from cell to cell, and special instructions are executed, which convert the whole into a type of material with a mantissa and exponent, as it should be, and return value. This is not a conversion, it is converted. Anyway, It will deal with the processor.

In this example, the math co-processor converts the real to integer, and will drive to a variable, and then do the reverse operation: It will drive a whole in his special register (It called ST), and after he would get from a real integral part, equal to that, that in variable “and”.

If you want to convert from a real double (8 bytes) in float (4 bytes), here such problems, as a whole will not be a. Math coprocessor just round up to the real value, fits in 4 bytes, that does not lead to an incorrect value:

The result will still be the number 1.234568e + 18, which means a 1.2-plus 18-degree. Another thing, what in “a” there will be rounded to the number of the 6th decimal place, instead 1.234567891234566e + 18. In general, inaccurate calculations it does not interfere, but it is recommended to use the program material of the same type, that did not happen here such rounding.

In addition to the type of transformation there is such a thing as the cast.

Visibility typecast can show such an example:

If the conversion – this decision C ++ (or rather compiler), in what type of result, it uses a type cast, rigidly specified by the programmer. In this example, the programmer tells the compiler, you need to convert it into a int, but not in short int. After all 25 and placed in int and short, that selects the compiler programmer may not know.

In everyday life such type conversions actually rare. And it's not necessary. The ideology of programming in a residential hints to avoid such frills, because they can be a rake (in the above example in the conversion of whole), therefore casts in C ++ should not be used everywhere.

:) Thank you for this article its author Stilet – Super Moderator programmers offline ProgrammersForum.

Newsletter of programming:

Conversion types
3.7 (73.33%) 3 votes

7 thoughts on “Conversion types

  1. This lesson is not clear, to rewrite this short article more understandable language:(

    1. A more clear in what terms?
      Type conversion after running at a low level and generally depends very strongly on optimizing compiler.
      Furthermore type conversion is very vast area and depends (may depend) from the tasks themselves, where the transformation is applied.

      1. > Type conversion after running low
        1. Transformations do not type “work” – no action is taken, we simply indicate to consider the element data in the format of a data type.

        > all depends very much on optimizing compiler.
        2. It does not depend at all on the compiler. typification – it is purely syntactic notion of C ++ – for monitoring and verification in the text. After compiling all of the types of information is completely lost.

  2. The data type of the object (as a noun is a language) determines what actions (verb in the language) can be applied to the object.
    For example, type – “an Apple”, it can be: eat, to dry, etc..
    But if you convert it to the type of “ball”, then it is possible to play football … but it can not eat.

    1. This is a false statement, because after typecast the value of their properties does not change.

      But I see no reason to argue – The debugger will judge all, who wants his aid it is to know the truth.

      1. > because after typecast the value of their properties does not change.

        You write nonsense, do not confuse adolescence…
        unsigned long ul = 1234;
        – transform:
        char *pc = ++(char*)the;
        how much will? … correctly ;-) = 1235
        – transform:
        short *ps = ++(short*)the;
        how much will? … correctly ;-) = 1236
        – transform:
        uint32_t* pi = ++(uint32_t*)the;
        how much will? … correctly ;-) = 1238

  3. 1) Type conversion on the example of a real and can run on the processor level and the math module.

    2) Typing is not just “notion”, and certainly not only for the control of, which in C as a cat laugh. Even if the type information is not stored in the executable, only matter, processor is uniquely indicates the type whether it is a pointer to a real or ASCIIZ, or interface pointer.

Leave a Reply

Your email address will not be published. Required fields are marked *