Data types in the C language

Source: Internet
Author: User

In the programming process, different CPUs, the meaning of their data types are not the same, so be sure to pay attention to the corresponding variable data type definition and conversion, otherwise there may be an indeterminate error in the calculation.

(i) Types of data in the C language

Integral type: int short long

Real type: float double

The data types in STM32 are very many, commonly used variables, which are defined in the file as follows:

/* Exact-width signed integer types */
typedef signed Char int8_t;
typedef signed short int int16_t;
typedef signed INT int32_t;
typedef signed __INT64 int64_t;

/* exact-width unsigned integer types */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __INT64 uint64_t;

typedef int32_t S32;
typedef int16_t S16;
typedef int8_t S8;

typedef uint32_t U32;
typedef uint16_t U16;
typedef uint8_t U8;

There is also a float int compiler that cannot see its definition (estimated compiled).

Therefore, in STM32 programming, the commonly used data types are: char (character type), U8,u16, u32, but in some calculations, involving negative numbers, decimals, so to use: int float doulbe type.

where u8--1 bytes, unsigned type (can not express negative numbers, if used as a negative number, the error);

U16--2 bytes, unsigned (see definition in front STM32f10x.h);

u32--4 bytes, unsigned type;

Int--4 bytes, signed type, can be expressed as a negative integer;

Float--4 bytes, signed type, can express negative/fractional;

Double--8 bytes, signed, can express negative numbers/decimals;

(ii) Mixed operations of different types of data

In C language, different types of data can be mixed between the operations. When the operation is performed, the different types of data are converted to the same type first, and then the operation is performed. The rules for conversion are as follows:

Note: The direction of the arrows only represents the level of the data type, converted from low to high, and the conversion process is one step.

(iii) data type conversion rules

The conversion of various types of data is divided into two ways: implicit (compiled software auto-complete), explicit (program-cast)

Implicit conversion rules:

The character must first be converted to an integer (the C language specifies that the character type data and the integer data can be generalized)
Type short converted to int (same as integral type)
Float type data is converted to double type in operation, to improve the precision of operation (same as the real type)
When assigning a value, the right value is converted to the left-hand type
Note
When the integer and double data are calculated, C converts the integer data to the double data and then the result is the double-precision type data
When the character data and the real data are computed, C converts the character data to the Narimi type data, then calculates it, and the result is the real data

Explicit conversion Rules:

Example: (int) (x+y);

Note: When you force a type conversion, you get a required intermediate variable, and the type of the original variable does not change.

Data types in the C language

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: info-contact@alibabacloud.com 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.