C Data Type

Source: Internet
Author: User

C Data Type

C Data Type

Basic Type

Value Type

Integer

Short

Integer int

Long Integer

Floating Point Type

Single precision float

Double Type

Char

 

 

Construction type

Array

 

 

Struct

 

 

Union

 

 

Enum

 

 

Pointer type

 

 

 

Void

 

 

 

 

 

Symbol attributes

Length attribute

Basic Type

Bit Length (bytes)

Value Range

Input character

Output character

--

--

Char

1

-2 ^ 7 ~ 2 ^ 7-1 (or 0 .. 255, related to the architecture)

% C

% C, % d, % u

Signed

--

Char

1

-2 ^ 7 ~ 2 ^ 7-1

% C

% C, % d, % u

Unsigned

--

Char

1

0 ~ 2 ^ 8-1

% C

% C, % d, % u

--

Short

[Int]

2

-2 ^ 15 ~ 2 ^ 15-1

% Hd % hi

 

Signed

Short

[Int]

2

-2 ^ 15 ~ 2 ^ 15-1

% Hd % hi

 

Unsigned

Short

[Int]

2

0 ~ 2 ^ 16-1

% Hu % ho % hx

 

--

--

Int

2/4

-2 ^ 31 ~ 2 ^ 31-1

% D % I

 

Signed

--

Int

2/4

-2 ^ 31 ~ 2 ^ 31-1

% D % I

 

Unsigned

--

Int

2/4

0 ~ 2 ^ 32-1

% U % o % x

 

--

Long

[Int]

4/8

-2 ^ 31 ~ 2 ^ 31-1

% Ld % li

 

Signed

Long

[Int]

4/8

-2 ^ 31 ~ 2 ^ 31-1

% Ld % li

 

Unsigned

Long

[Int]

4/8

0 ~ 2 ^ 32-1

% Lu % lo % lx

 

--

Long

[Int]

8

-2 ^ 63 ~ 2 ^ 63-1

% I64d % lld % Ili

 

Signed

Long

[Int]

8

-2 ^ 63 ~ 2 ^ 63-1

% I64d % lld % Ili

 

Unsigned

Long

[Int]

8

0 ~ 2 ^ 64-1

% I64u % I64o % I64x

 

--

--

Float

4

+/-3.40282e + 038

% F % e % g

 

--

--

Double

8

+/-1.79769e + 308

% Lf % le % lg

% F, % e, % g

--

Long

Double

8 or above

+/-1.79769e + 308

% Lf % Le % Lg

 

 

Note:

1. Each row in the table represents a basic type. "[]" Indicates that it can be omitted.

2. char, signed char, and unsigned char are different types. int, short, and long are also different types.

3. When char/signed char is converted to int, the maximum sign bit 1 is extended, resulting in operational problems. Therefore, if the data to be processed contains a byte value greater than 127, it is more appropriate to use unsigned char. If bit operations are involved in the program, the unsigned variable should also be used.

4. whether the length of the int is 16-bit or 32-bit depends on the compiler length.

5. integer data can be % d (in hexadecimal notation), % o (in hexadecimal notation), or % x/% X (in hexadecimal notation) input and output. The format character % u indicates unsigned, that is, the hexadecimal mode without symbols.

6. The integer prefix h indicates short, and the l indicates long. When the input and output short/unsigned short, we do not recommend that you directly use the int format, such as % d/% u, with the prefix h. (Input and output data types in a matched format)

7. For the input and output of the long type, "% lld" and "% llu" are gcc/g ++ for the long int type (64 bits) in linux) the format of the input and output. While "% I64d" and "% I64u" are the format descriptions used in the Microsoft VC ++ library for input and output _ int64 type.

8.% f, % e/% E, or % g/% G can be used for float data input. The scanf function will automatically process the data based on the input data format. You can use % f (normal mode), % e/% E (exponential mode), or % g/% G (automatically selected) for output ).

9. Floating Point parameter pressure stack rules: float (4 bytes) type is extended to double (8 bytes) into the stack. During input, float (% f) and double (% lf) need to be distinguished; During output, use % f. The printf function outputs the float (extended to double) and double data pushed into the stack according to the double-type rules. If the % lf format character is specified during output, the gcc/mingw32 compiler will give a warning.

10. prefix L indicates long (double ). Although long double is 4 bytes longer than double, the value range is the same. The length, precision, and representation range of the long double type are related to the compiler and operating system used.

 

64-bit integer full solution

The confusion caused by 64-bit shaping mainly involves two aspects: one is data type declaration, and the other is input and output.

First, if we write programs on our own machine, the situation is classified as follows:

 

(1) In VC6.0 under win, when declaring the data type, you should write

_ Int64;

% I64d is used for input and output.

Scanf ("% I64d", & );

Printf ("% I64d", );

 

(2) In gcc/g ++ in linux, data type declaration writing

Long;

% Lld is used for input and output.

 

(3) In other ides under win, [including Visual Studio of higher versions], the data type declaration can be either of the above two types.

% I64d for Input and Output

 

Data Type Conversion

Data type conversion is to convert data (results of variables and expressions) from one type to another.

Forced type conversion: explicit conversion by the programmer.

Automatic type conversion: the compiler implicitly converts data types based on different data types in the hybrid operation.

 

Automatic type conversion rules:

 

Note:

1. char and short types must be converted to int type before calculation.

2. In the value assignment operation, the Data Types on both sides of the value assignment number are different. You need to convert the type of the right expression to the type of the Left variable. If the data type length of the expression on the right is longer than that on the left, part of the data will be lost, which will reduce the precision.

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: 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.