The purpose of forced type conversion in C language, the basic format, the essential meaning of variables in C, and the essence of Variables

Source: Internet
Author: User

The purpose of forced type conversion in C language, the basic format, the essential meaning of variables in C, and the essence of Variables
C forced type conversion

Reading directory:

1. Purpose and basic format of forced type conversion

Ii. Essential Meaning of variables in C

Iii. Forced type conversion of common variables

Iv. pointer variable type conversion

1. Purpose and basic format of forced type conversion:

1.1 strong conversion Purpose
C language is a strongly typed language. If an operator has different data types during two operations, convert it to the same type first. Forced type conversion can eliminate warnings in the program, that is to say, make sure that the programmer who writes the code knows the type conversion, and allow loss of precision or type matching.
For example:

// 1. Loss of accuracy
int main (void)
     float f = 5.75;
     printf ("f =% d, f =% f \ n", (int) f, f);
// Result: f = 5, f = 5.750000

// 2 、 Do type matching
int main (void)
     int a [5] = {1, 2, 3, 4, 5};
     int * p = (int *) (& a + 1);
     printf ("* (p-1) =% d. \ n", * (p-1));
// Result: * (p-1) = 5
// Inside & a + 1 + 1, the address plus sizeof (a) is the length of an array
// (int *) converts a pointer to an array type to a pointer to an integer, and the assignment operators correspond left and right

1.2 Basic Format of Medium-strong transfer in C

The method is simple, as shown below:
A = (TYPE1) B; // forced conversion.

Ii. essential meanings of variables in C:

(1) All types of data in c are stored in the memory in binary format. Therefore, the memory only knows that there are 0 and 1, and whether it is int, float, or other types.

(2) int, char, and short belong to the integer type, and their storage methods (the data is converted to binary and stored in the memory) are the same, only the size of the memory lattice is different (so these shaping types are called each otherBinary compatible format); Float and double storage methods are different from each other, and they are different from integer storage.

(3) The essence of the Data Type in C language determines how the data is stored in the memory, that is, how the data is converted to binary. One thing to remember is that the memory only stores the sequence of 1010, regardless of how these 1010 are parsed. Therefore, we need to ensure that the data types are consistent in normal times.

Int a = 5;

Storage:The compiler allocates a 4-byte space and converts 5 to binary Storage Based on the int type to store it in the memory space corresponding to a (a is left );

Valid value:When printf prints ), the vsprintf function in printf parses the memory space of a according to the format string (that is, % d in the first string parameter of printf, the parsed value is used for output.

Resolution rules:Stored according to the Data Type of the variable itself (for example, in this example, a is int and therefore stored in the int format ); however, the format of the formatted string such as % d in printf is used for extraction. At this time, although the 10101 sequence in the memory space represented by a has not changed (the memory is not modified), it is not certain how to understand (how to convert these 1010 to numbers. For example, if % d is used for parsing, the value is still 5 if it is parsed in int format. However, if % f is used for parsing, therefore, printf assumes that the memory space of a is a float type and will be parsed according to the float type. The value is naturally a strange number.

// Data type access difference
int main (void)
     // 1. It will be wrong to store according to int type but take it according to float type.
     int a = 5;
     printf ("a =% d. \ n", a); // 5
     printf ("a =% f. \ n", a); // 0.000000

     // 2. Stored as int but fetched as char,

     int a = 6;
     char * p1 = & a;
     printf ("* p1 =% d. \ n", * p1); // 6 in the char range, no error

     short * p2 = & a;
     printf ("* p2 =% d. \ n", * p2); // 6

     int a = 66666;
     char * p1 = & a;
     printf ("* p1 =% d. \ n", * p1); // 106 error outside char and short range

     short * p2 = & a;
     printf ("* p2 =% d. \ n", * p2); // 1130


(1) int and char types are both integer and compatible. So sometimes, when switching, it's wrong and sometimes it's right.

(2) The difference between int and char is that char has only one byte and int has four bytes, so the int range is larger than char. Within the range indicated by char, int and char can be converted to int without errors. However, if the range is exceeded, conversion from char to int will not be wrong (if it is in the big direction, it will not be wrong, it's like taking the water from a small bottle to a large bottle will not be missed and won't be lost), and turning from int to char will make an error (as if taking the water from a large bottle to a small bottle is the same)

Iii. General variables conversion rules:

3.1 conversion rules for common variables
When the data of lower types is converted to a higher type, the form is changed, without affecting the actual content of the data, some data may be lost when the conversion from a higher type to a lower type. If the operation numbers on both sides of an operator are of different types, first convert them to the same type, that is, convert the value on the right side of the operator to the type on the left side of the operator, and convert the same-side Low-precision type to the high-precision type, the conversion rules are shown in.

Double comment ── float height
Int ── char, short is low

Horizontal arrowIt indicates a required conversion. For example, if two float types are involved in the operation, although they are of the same type, they must be converted to the double type before calculation. The result is also the double type.

Vertical arrowIt indicates the conversion when the number of operations on both sides of the operator is different types. For example, if a long data type is used together with an int data type, you must first convert the int data to the long data type, the conversion can be said to be automatic, but try to use an explicit mechanism to force the conversion type (make sure that the conversion is allowed.

3.2. Specific conversion details

(1) floating point type and integer type

When you convert a floating point (single-and double-precision) to an integer, the fractional part of the floating point is discarded and only the integer part is retained. Assign the integer value to the floating point variable. The value remains unchanged. Only the floating point type is changed to the floating point type. That is, the decimal point is followed by several zeros. Note: The type conversion when values are assigned is actually mandatory.

(2) Single and Double Precision Floating Point

Because the floating point values in the C language are always expressed in double precision, float data only adds 0 at the end and is extended to doub1e for calculation, and then directly assigns values. When data of doub1e type is converted to float type, it is achieved by the number of truncated tails. Rounding is required before truncation.

(3) char and int types

When an int value is assigned to a char variable, only the minimum 8 bits are retained. When a char value is assigned to an int type variable, Some compilers process positive numbers regardless of the value size. When another Compilation Program is converting, if the char type data value is greater than 127, it is processed as a negative number. For the user, if the original char data is positive, the conversion is still positive. If the original char value can be positive or negative, the original value remains unchanged after conversion, the internal representation of data is different.

(4) int and 1ong

When long data is assigned to an int type variable, the 16-bit lower value is sent to the int type variable, and the 16-bit higher value is truncated. (Assume that the int type occupies two bytes ). When int type data is sent to long type variables, the external value remains unchanged, while the internal form changes.

(5) unsigned integer

When an unsigned data is assigned to an integer variable that occupies a storage unit of the same length (for example, unsigned → int, unsigned long → long, unsigned short → short), the original value is assigned, the internal storage method remains unchanged, but the external value may change.

It is a good habit to use forced type conversion. In this way, you can at least see what you want from the program.

Iv. Rules for strong conversion of pointer variables:

(1) A strong conversion of pointer variables changes the ability of pointer addressing.

(2) A pointer involves two variables: one is the pointer variable itself, and the other is the variable pointed to by the pointer variable.

(3) intP; when defining pointer variables, p (pointer variable itself) is intType. * p (the variable pointed to by the pointer) is of the int type.

(4) intTo put it bluntly, the pointer type occupies 4 bytes as long as it is pointer type, the resolution method is based on the address (meaning that the 32 binary values in the memory add up to a memory address. The conclusion is: all pointer types (whether intOr char * or double.

(5) For the variable pointed to by the pointer, the pointer type is very important. The type of the variable pointed to by the pointer (the parsing method of its memory space) depends on the pointer type. For example, if the pointer is int *, the variable pointed to by the pointer is of the int type.

int main (void)

     int a [3] = {0x11223344, 0x55667788, 0};

     int * p1 = a;
     printf ("* p1 = 0x% x \ n", * p1); // * p1 = 0x11223344

     char * p2 = (char *) a; // change the addressing capability
     printf ("* p2 = 0x% x \ n", * p2); // * p2 = 0x44
     printf ("* p2 = 0x% x \ n", * (p2 + 1)); // * p2 = 0x33
     printf ("* p2 = 0x% x \ n", * (p2 + 2)); // * p2 = 0x22
     printf ("* p2 = 0x% x \ n", * (p2 + 3)); // * p2 = 0x11
     printf ("* p2 = 0x% x \ n", * (p2 + 4)); // * p2 = 0xffffff88?
     printf ("* p2 = 0x% x \ n", * (p2 + 5)); // * p2 = 0x77

     return 0;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.