expression and application of "c language" constants and variables

Source: Internet
Author: User

First release, but also in the revision of the perfect.

In a program run, the amount whose value cannot be changed becomes a constant. In the basic data type, constants can be divided into integer constants, real constants, symbolic constants, and character constants (including character constants and string constants), which are described as follows:

Integral type constant

The integer constant, consisting of one or more digits, which can be signed

Integer constants in the C language can be expressed in decimal, octal, and hexadecimal 3 forms

Decimal integer: Consists of 0~9 digits, cannot start with 0, no prefix

Octal integers: prefixed with 0, followed by 0~7 numbers, with no fractional component

Hexadecimal integer: Beginning with 0x or 0X, followed by 0~9 's number and a~f (or a~f letter)

Additional Long integer constants are appended with the suffix L (or 1), and the unsigned constant is appended with the suffix U (or u)

Real-type constants

In C, real numbers are also called floating-point numbers, and there are generally two representations of them.

Decimal decimal form: Consists of a number and a decimal point, which must have decimal points (for example,. 24 also valid)

Exponential form: There is a decimal number, the order symbol "E" or "E" and the Order of the composition, the order code can only be integers. There must be numbers in front of E and E, followed by integers

Types of Real constants: c The compiler system does not divide the real constant by the float and double, both as double processing to be more precise, or you can add the letter F or F after the real constant to specify that it is a single precision (float)

Symbolic constants

In C, you can use an identifier (the concept of an identifier as the end of the text) to represent a constant, called a symbolic constant, which is a constant of the identity form

A symbolic constant is a special constant whose value and type are determined by the definition of a symbolic constant.

Symbolic constants must be defined before they are used, and the general form is as follows:

#define Identifier Constants

(#define是一条预处理命令, its function is to define the identifier in the command format as the constant value thereafter)

Traditionally, in order to differentiate itself from variable names in a program, symbolic constant names are generally denoted by uppercase letters.

Character-type constants

Include character constants and string constants

Character constants:

Also known as a character constant, character constants in C are characters enclosed in single quotes, case-sensitive

Character constants have the following characteristics:

(1) Character constants can only be enclosed in single quotes.

Single quotes are only defined and do not represent the character itself

Only one character can be used in a single quotation mark, and a character may be any character in a character set

The characters in single quotes cannot be single quotation marks (') and backslashes (\)

(2) Each character constant has an integer value, which is the ASCII code of the number (see Appendix in the Code table)

(3) Character constants are case-sensitive

For some commonly used but difficult to represent non-display characters, C provides a special form of character constants, a sequence of characters that begins with an escaped identifier "\" (a backslash), such as a table (an escape character and its meaning):

String constants:

String constants are sequences of strings enclosed in pairs of double quotes

The C language specifies that string constants are stored in the following ways:

Each character in the string is stored in memory with its ASCII code, and the system automatically adds a "string end flag" to the end of the string

(' + ', which is the ASCII code value of 0 characters, it does not cause the person and the control action, also is not to display the character)

So that the system determines whether the string ends. Example: The string "A", the actual length is 2, contains ' a ' and '

Variable

Variables are only the amount that can be changed in a program, and once defined, there are 3 basic elements: variable name, variable type, and variable value

Variable name:

Naming rules for variables (see appendix)

Variable type:

In C, variables follow the "define before use" principle, and the variables are defined in the following form:

Variable type variable table name

Where the variable type is the data type stored in the variable, the data type concept is described in the previous blog "C Language" data type and mixed operation and type conversion

Integer variable

Real variables

Character variables

Variable Value:

In a program, a variable must be preceded by a certain value before it can participate in a variety of appropriate operations

A variable can get a value from an assignment statement or input statement, or it can be initialized to get a value

C language Identifiers

A valid sequence of characters used to identify a variable name, symbolic constant name, function name, type name, and file name is called an identifier.

    1. Identifiers consist of a letter (a-z,a-z), a number (0-9), an underscore "_", and the first character cannot be a number, but it can be a letter or an underscore. For example, the correct identifier: abc,a1,prog_to.
    2. You cannot use the C keyword as a user identifier, such as if, for, while, and so on.
    3. Identifier length is determined by the compiler system on the machine, the general limit is 8 characters (note: 8 character length limit is C89 standard, C99 standard has been extended length, in fact, most industrial standards are longer).
    4. Identifiers are case sensitive, which is strictly case-sensitive. The name of the variable is generally lowercase, and the symbolic constant is capitalized.
    5. The name of the identifier should be " known as", for example, length (Foreign language: length), Sum, Total (Foreign language: sum), pi (Foreign language: PI) ...
    6. The C language divides identifiers into three categories: keywords, predefined identifiers, and user-defined identifiers [1] .

ASCII code

expression and application of "c language" constants and variables

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.