Data Type of C Language

Source: Internet
Author: User
Tags print format

Data Type of C Language

Take notes <data type in C Language>

I. Integer Data Type

(1) Integer Data Type

Integer type

Char bytes variable 1 byte (8 bit)

Short integer 2-byte (16 bit)

Int integer variable depends on the Compiler

Long Integer Variables depend on the Compiler

For intUnderstanding: There are CPU and memory in the computer, there are registers in the CPU, and data transmission between the CPU and memory through the bus, the so-called Number of CPU bits (such as 32-bit, 64-bit) it refers to the size of a register, the size of the register, and the size of the bandwidth that the bus can process. For example, the size of the register of a 32-bit machine is 32-bit, and the data it processes at a time is 32-bit, the int size is 32Bit, which is 4 bytes.IntThe size depends on the register size..

The specific int length can be a function.Sizeof (int).

(2) Data Storage Format

Data is involved in calculation in binary form in a computer, and data has positive and negative values. This requires the design of a data storage form by meansSupplemental form.

For example:

Take the char type as an example. If it is represented by eight bits, 0 can be represented by 0000 0000, and 1 can be represented by 0000 0001, since-1 = 0-1,-1 can be expressed as 0000 0000-0000 0001, and the result is 1111 1111. The high-order carry is directly eliminated. This is the supplemental code.

So the range of char variables is-128 ~ 127, of which 0111 1111 is 127,1111 1111 is 128.

The above is a signed variable. The C language also provides the unsigned form variable, which can be added before the variable formUnsignedUnsigned form.

The purpose of the unsigned variable is to be able to perform binary shift operations (because the highest bit is symbolic when there is a symbolic form ).

(3) Data input and output form

CThe language data adopts printf.Function to print data, The usage is as follows:

Printf ("data is % d", a); // There are multiple entry parameters. The first parameter is the data format, that is, the print format, the content of the example is to print "data is % d", where % d is a special format, representing an integer. As for the data content, the following parameter is given. In this example, it is, when a = 10 is added, the data is "10" on the screen"

For % f, % DOther data formats will be introduced later.

(4) Data selection

We define what type of data to select. If you have no special requirements, selectIntThe current computer operations are both 32-bit and 64-Bit Data Processing digits are 32-bit. Even if you declare the char type, it will not save space and time. On the contrary, it may increase the usage time.

Summary:This section mainly describes the types of integer data and their existence in the computer. Remember the int representation. Because of this, the portability of the C language is poor, while the JAVA language does not have strict requirements on variable types, and the int Data Length remains unchanged, therefore, Android is often written in JAVA (benefiting from its portability ). The C language's unsigned makes it easy to shift, so it is widely used in embedded single-chip microcomputer.

Ii. Floating Point Data Type

The floating point type is the number of decimal places that are common in life. However, in the computer, the floating point processing is very special. Integers exist in the computer in the form of binary, rather than floating-point numbers.Floating-point numbers are encoded numbers. Therefore, two floating-point numbers cannot be directly computed in binary mode in the computer. They usually require special hardware support.

The computer expression of floating point numbers has caused many interesting floating point numbers.

(1) Floating point data type

Float

32-bit

± (1.2E-38 ~ 3.4E + 38)

Double

64-bit

± (2.2E-308 ~ 1.79E + 308)

Note:: The table shows that the data represented by floating point data can only be near zero, but cannot represent 0. The encoded floating point data in the computer is fixed. We will give a data, he will encode the data according to the distance from the data. For example, the command float f = 0.002 may not contain the data in the floating point data in the computer, and only 0.002001 and 0.002005 of the data will automatically change our data to 0.002001.

(2) Floating point numeric judgment

For floating-point numbers, the calculation result of a and B may be 0.5 because of different numerical precision, but the binary form of the two floating-point numbers may be different.

So determine whether the numbers are equal and determine the following equations

Fabs (F1-F2) <1e-12

The computing of floating point numbers is based on the internal CPU hardware.

 

Iii. Logical Data Types

(1) Logical Data Type

When the data type is used for logical judgment, only true and false values are 0 and 1 in the code. In computing,CPUThink 0Is false, non-zero is true. The 0 value of the integer variable in C language indicates false, the non-zero value indicates true, and the logical value is 1.

Bool is a logical variable, which is included in stdbool. h. If this header file is included, you can use bool. However, its usage is not big, because it is still an integer variable, but there are two true and false values as follows.

Bool a = true; // declare logical variable a = 1

Printf ("% d", a); // The output result is 1

 

(2) Logical operation

Operator number

Description

Instance

Explanation

!

Non

! A

If a is 0, the result is 1. If a is non-zero, the result is 1.

&&

Logic and

A & B

When both numbers are 1, the result is 1, and the rest are 0.

|

Logic or

A | B

When both numbers are 0, the result is 0, and the rest are 1.

&

Bitwise AND

A & B

Bitwise AND refers to expanding two variables into binary, and performing operations on each bit.

|

By bit or

A | B

 

Note::

(1) When the bitwise AND operation is performed, when the value of a is already false, if the result is confirmed as false, that is, 0, B is not required. For example (a = 0) & (B = 2) the result is 0 and a is assigned 0, but B is not assigned 2. Similarly, when bitwise AND operation is performed, if the value of a is already true, the result is determined to be true. That is, if the value of 1 is true, B is not required.

(2) The priority of the single object operator is higher than that of the binary operator, for example! Age <20 indicates that age is not, and then evaluates whether it is less than 20.

 

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.