Dark Horse Programmer C Language: Data type

Source: Internet
Author: User
Tags sin

The data type of the C language

The various variables used in the C language program should be described in advance, that is, before use. The description of a variable can include three aspects:

• Data type
• Storage Type
• scope

The so-called data types are divided by the nature of the stated quantity, the representation form, how much storage space is occupied, and the tectonic characteristics. In c language, data types can be divided into: basic data type, construction data type, pointer type, empty type four categories.

1. Basic data types
The main feature of the basic data type is that its value can no longer be decomposed into other types. In other words, the basic data type is self-explanatory.

2. Construct data type construct data type
is defined by a constructed method, based on one or more defined data types. That is, a value of a constructed type can be decomposed into several "members" or "elements". Each "member" is either a basic data type or another constructed type. In the C language, there are several types of constructs:
• Array type • Structure type • union type
3. Pointer type
Pointers are a special, but also important, type of data. Its value is used to represent the address of a quantity in the internal memory. Although the value of a pointer variable is similar to an integer, this is a completely different amount of two types and cannot be confused.

4. Empty type

When calling a function value, a function value should normally be returned to the caller. The returned function value is of a certain data type, and should be given in the function definition and function description, for example, in the definition of the MAX function in an example, the function head is: int max (int a,int b), where the "int" type specifier means that the return value of the function is an integral type. As in the example, the library function sin is used, because the system specifies that its function return value is a double-precision floating-point type, so in the assignment statement S=sin (x), s must also be a double-precision floating-point type in order to match the return value of the SIN function. So in the Description section, describe S as a double-precision floating-point type. However, there are also a class of functions that, when called, do not need to return a function value to the caller, which can be defined as "null type". Its type specifier is void. In the fifth chapter of the function is also described in detail. In this chapter, we first describe the integer, floating-point, and character types in the basic data type. The remaining types are introduced in later chapters.
For the amount of the basic data type, whether the value can be changed and divided into constant and variable two. During program execution, the amount of the value that does not change is called a constant, and the variable value is called a variable. They can be categorized in combination with data types. For example, you can divide integer constants, integer variables, floating-point constants, floating-point variables, character constants, char variables, enumeration constants, and enumeration variables. In a program, a constant can be directly referenced without a description, whereas a variable must be used before it is explained.

Integral type Quantity
Integral types include integer constants, integer variables. Integral constants are integral constants. In the C language, the integer constants used are eight hexadecimal and three decimal.

Integral type constant

1. Eight binary integer Constants

The octal integer constant must begin with 0, which is prefixed with 0 as the octal number. The digital value is 0~7. The octal number is usually an unsigned number.

2.16 Binary integer Constants
The hexadecimal integer constant is prefixed with 0X or 0x. Its digital value is 0~9,a~f or a~f.

3. Decimal integer constant
The decimal integer constant has no prefix. Its digital is 0~9.

Integer variable

Integer variables can be divided into the following categories:
1. Basic type
The type descriptor is int, and its value is the basic integer constant.

2. Short and full volume
The type descriptor is short int. Both the byte and the value range are the same as the basic type.
3. Long Integer type
Type descriptor is long
An int or long whose value is a long integer constant.
4. Unsigned type
The type descriptor is unsigned.

Amount of real type

Real-type constants

A real type is also called a floating-point type. A real constant is also called a real or floating-point number. In the C language, the real number is only used in decimal. It has two different forms:
Decimal number form exponent form

Real variables

There are two types of real variables: single-and double-precision

Character Type amount

The character types include character constants and character variables.

A character constant character constant is one of the characters enclosed in single quotation marks. For example ' A ', ' b ', ' = ', ' + ', '? ' are all legal character constants.

Constants have the following characteristics:

1. Character constants can only be enclosed in single quotes, not double quotes or other parentheses.
2. A character constant can only be a single character and cannot be a string.
3. The character can be any character in the character set. But after the number is defined as the character type,
Cannot participate in numeric operations. such as ' 5 ' and 5
is different. ' 5 ' is a character constant and cannot participate in the operation.

Escape character
The escape character is a special character constant. The escape character begins with a backslash "\" followed by one or several characters. The escape character has a specific meaning, which differs from the original meaning of the character, so it is called an "escape" character.

Character variables
The value of a character variable is a character constant, which is a single char. The type specifier for a character variable is char. Character variable type descriptions have the same formatting and writing rules as Integer variables.

String constants
A string constant is a sequence of characters enclosed by a pair of double quotation marks. For example: "China", "C Program:", "$12.5"
And so on are all valid string constants. String constants and character constants are different amounts. The main differences between them are:
1. Character constants are enclosed in single quotes, and string constants are enclosed in double quotation marks.
2. Character constants can only be single characters, and string constants may contain one or more characters.
3. You can assign a character constant to a char variable, but you cannot assign a string constant to a character variable. There is no corresponding string variable in the C language. This is different from the basic language. However, you can use a character array to hold a string constant. Introduced in the chapter of the array.
4. Character constants account for one byte of memory space. The number of bytes in a string constant is equal to the number of bytes in the string plus 1. Adds a byte that holds the character "s" (ASCII code is 0). This is the flag of the end of the string.

Initial and type conversions of variables

Variable Assignment initial value
In a program, you often need to assign an initial value to a variable in order to use it. There are several ways in a language program that can be assigned an initial value when defined, and this method is called initialization. The general form of assigning an initial value in a variable description is:
Type specifier variable 1= value 1, variable 2= value 2, ... ;

Conversion of variable types
The data type of the variable can be converted. There are two ways of converting, one is auto-conversion and one is casting.

Dark Horse Programmer C Language: Data type

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.