C/C ++ data type (Summary of online data)

Source: Internet
Author: User

1. The integer types in C language include char, short, Int, long, etc. The following section describes the C language's requirements for the length of each data type:
(A). The short and long types have different lengths.
(B) The Int type is usually the same as the physical character length of a specific machine.
(C ). short is usually 16 bits, and Int Is usually 16 bits or 32bits. Each compiler can be determined based on different hardware, but short and INT must be at least 16 bits, the long type must be at least 32 bits, and the short type must be shorter than the int and long types.
2. the sizeof () operator returns the number of bytes contained in a data type (bytes). ansic requires that sizeof (char) must return 1. When sizeof acts on an array, returns the number of bytes occupied by all the members in the array (note that it is not the number of members in the array). When sizeof () Acts on the struct and the public body, the returned value is not only the total number of bytes of data members, but also the bytes that the compiler fills in to implement the bytes.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------

C ++ data type length problem:
1. bytes and length
Bytes. Eight bits are a fixed concept. Word Length refers to the length of binary data that can be processed by a computer at a time. It is a non-fixed concept. For example, the length of an 8-bit computer is 8, that is, one byte. The length of a 32-bit computer is 32, that is, four bytes. Similarly, the length of a 64-bit computer is 64, 8 bytes.
Ii. Data Types in C ++
1. Character-type data char, which is always an 8-bit long byte.
2. Integer: int, short, and long. Generally, Int Is a word length, short is half a word length, and long is one or two characters long (a word length in a 32-bit machine ).
3. Float, double, and long double indicate Single-precision floating-point double and Extended Floating-point respectively. In typical cases, float is a word, double is two words, and long double is two or three or four words.

 

 

How long are the char, short, Int, and long types?
The length is one byte, at least two bytes, at least two bytes and at least four bytes. In addition, do not rely on any conventions.
The length of the char type is defined as an 8-bit byte, which is very simple.
The short type must be at least two bytes in length. On some computers, for some compiled programs, the short type may be 4 bytes in length or longer.
The Int type is the "natural" size of an integer. The length must be at least two bytes and at least the same length as the short type. On a 16-bit computer, the length of the int type may be two bytes; on a 32-bit computer, it may be four bytes. When a 64-bit computer becomes popular, the length of the int type may be 8 bytes. All of this is "possible". For example, the early motorala 68000 was a 16/32-bit Hybrid computer that relied on different command line options, A 68000 compilation program can generate two-byte long or four-byte long int type.
The long type is at least as long as the int type (so it is at least as long as the short type ). The length of the long type must be at least 4 bytes. The compiled program on a 32-bit computer may make the short, Int, and long types have four bytes in length-or not.
If you need a 4-byte long integer variable, do not assume that the Int or long type can meet the requirements, to use typedef to define an inherent type (a type that does exist) as your desired type, and add the corresponding # ifdef command before and after it:
# Ifdef four_byte_long
Typedef long int4;
# Endif
If you need to write an integer variable to a file or network in byte stream mode, and then read it from different computers, you may use this type (if you want to do so, see 15.5 ).
If you need a two-byte long integer variable, you may have some trouble! Because this type does not necessarily exist. However, you can always store a small value in an array consisting of two Char Types.

 

 

Integer Data Types int, short int, and long int in C Language

1. Range: Occupied Space

According to the standard, int indicates the rangeCannot be lessShort indicates the range, and long indicates the range.Cannot be lessInt. This means that the short variable may occupy less space than the int variable, while the long variable may occupy more space than the int variable.

In a 16-bit computer, int and short are generally 16 bits, while long is 32 bits;

In 32-bit computers, short is generally 16 bits, while long and INT are 32 bits. In TC2 (16-bit compiler), Int Is 16 bits, while in Dev-C ++ (32-bit compiler), Int Is 32 bits.

Ii. 16-bit compiling environment:

 

Name

Full name type description Abbreviation type description Number of digits Range
Integer Int Int 16-bit -32768 to + 32767
Unsigned integer Unsigned int Unsigned 16-bit 0 to 65,535
Short integer Short int Short 16-bit -32768 to + 32767
Unsigned short integer Unsigned short int Unsigned short 16-bit 0 to 65,535
Long Integer Long int Long 32-bit -2,147,483,648 to 2,147,483,647
Unsigned long integer Unsigned long int Unsigned long 32-bit 0 to 4,294,967,295

3. There are three different forms of Integer constants in C language:

1. octal integer constant: add the prefix "0" before the octal value, and the digital value is 0 ~ 7. For example, 054, 0567, and 05421.

2. hexadecimal integer constant: the prefix is "0x" or "0x", and its digital value is 0 ~ 9. ~ F or ~ F. For example, 0x3d, 0xe0, and 0xfff.

3. Decimal integer constant: no prefix or suffix. For example, 254, 745, and 890. If the string type is long, a letter l or l must be attached at the end of the numeric sequence as the suffix. Example: 245l, 7850l, 124l, etc.

 

4. Selection of integer types

If you want to handle onlyNon-negative integer, The unsigned integer type should be given priority. If the integer to be processed exceeds the range expressed by the int and the long value in your compiler is larger than the int value, the long value is used. However, if not necessary, do not use long because itMay reduce program running efficiency. Note:In your compiler, long and INT are both 32-bit, and you need to use a 32-bit integer, you should use long instead of Int.Only in this way can our program be securely transplanted to a 16-bit computer, because in a 16-bit computer, Int Is usually 16-bit. Similarly, if you want to use a 64-bit integer, use long. If the int value is 32 bits, using short can save space. However, make sure that the integer to be processed does not exceed the short value range. This "save" is meaningless for computers with large memory.

5. Long constants and long Constants
Generally, Integer constants are stored as int type. If the integer constant we use exceeds the int expression range, the C language requires the compiler to automatically use the unsigned int to process this constant. If unsigned is not enough to represent this constant, the compiler will use long. If not, use unsigned long, long, unsigned long in sequence. If unsigned long does not mean it, then the compiler will have nothing to do with it. Note: long and unsigned long are unique to c99. For example, if the int value is 16 bits, it cannot represent the constant 1000000. The compiler uses long to process this constant, because the unsigned int cannot represent 1000000.
Likewise, hexadecimal and octal Integer constants are usually processed as Int. However, when the constants we use exceed the int expression range, the compiler uses unsigned int, long, unsigned long, long, and unsigned long in sequence. Until the type used is sufficient to represent that constant.
Sometimes we use a small constant, but we want this constant to be processed as a long value. This requires the constant to be followedSuffixL (lower case letter L) or L (upper case letter L ). We should avoid using L, because l is easy to confuse with number 1. For example, an integer constant 7 is processed as an int, but an integer constant 7l (or 7l) is processed as a long. Similarly, after an integer constant is appended with the suffix ll or LL, the constant will be treated as long. Example: 3ll. To use an unsigned integer constant, use the suffix U or U. For example, 2u, 3u, 4lu, 5ul, 6lu, 7llu, 8ull, and 9ull.
These suffixes can also be used for hexadecimal and octal Integer constants. For example: 020l, 010ll, 0x30ul, 0x40ull.

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.