Expression and Application of constants and variables in C Language

Source: Internet
Author: User
Tags printable characters

Expression and Application of constants and variables in C Language

When the program is running, the amount of values that cannot be changed becomes a constant.

In the basic data type, constants can be classified into Integer constants, real constants, symbol constants, and character constants (including character constants and string constants:

 

Directory:

I. Constants

II. C language identifier

Iii. Variables

Appendix: ASCII code table

 

I. Constants

 

1. Integer constants

An integer constant, which is composed of one or more numbers and can contain positive and negative numbers.

Integer constants in C can be expressed in decimal, octal, and hexadecimal notation.

Decimal INTEGER: from 0 ~ 9 digits, cannot start with 0, no prefix

Octal INTEGER: prefixed with 0, followed by 0 ~ Composed of digits of 7, with no decimal part

Hexadecimal INTEGER: it must start with 0x or 0X and then start with 0 ~ 9 digits and ~ F (or ~ F letters)

In addition, the suffix L (or 1) is added after the long integer constant, and the suffix U (or u) is added after the unsigned constant)

 

2. Real Constants

In C, real numbers, also known as floating-point numbers, are generally expressed in two forms.

Decimal decimal form: it consists of digits and decimal points. It must have a decimal point (for example,. 24 is also valid)

Exponential form: it consists of a decimal number, level code mark "e" or "E", and level code. The level code can only be an integer. E and E must be preceded by numbers and must be integers.

Type of a Real-type constant: The C compilation system processes real-type constants, regardless of float or double to be more precise, you can also add the letter f or F after a real-type constant to specify it as a single-precision real-type (float)

 

3.Symbol constant

In C, you can use an identifier to represent a constant, which is called a symbolic constant.

Symbolic constants are special constants whose values and types are determined by the definition of symbolic constants.

A symbolic constant must be defined before use. The general form is as follows:

# Define identifier constant

(# Define is a preprocessing command. Its function is to define the identifier in the Command Format as its constant value)

Traditionally, to be different from the variable name in the program, the symbolic constant name is generally expressed in uppercase letters.

 

4. Constant type

Including character constants and string constants

(1) character constants:

It is also called a character constant. The character constants in C language are characters enclosed in single quotes and are case sensitive.

Character constants have the following features:

① Character constants can only be enclosed in single quotes

Single quotes only define the character, not the character itself

A single quotation mark can contain only one character. A character can be any character in the character set.

The characters in single quotes cannot be single quotes (') or backslash (\)

② Each character constant has an integer that is the ASCII code of the number (see the appendix)

③ Character constants are case sensitive

For non-printable characters that are commonly used but difficult to be expressed in the general form, C language provides a special form of character constants, that is, using an escape identifier "\" (backslash) character sequences starting with, such as tables (escape characters and their meanings ):

(2) string constants:

A String constant is a string sequence enclosed by a pair of double quotes.

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

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

('\ 0', a character with an ASCII code value of 0. It does not cause human and control actions, nor is it a printable character)

This allows the system to determine whether the string ends. For example, the actual length of the string "a" is 2, including 'A' and '\ 0'

 

 

II. C language identifier

 

An identifier is a sequence of valid characters used to identify variable names, symbol constant names, function names, type names, and file names.

1. An identifier consists of letters (A-Z, a-z), numbers (0-9), and underscores (_). The first character cannot be a number, but can be a letter or underline. For example, the correct identifier is abc, a1, prog_to.

2. You cannot use the C language keywords as user identifiers, such as if, for, and while.

3. the length of the identifier is determined by the compilation system on the machine. Generally, the length is limited to 8 characters (Note: The length of 8 characters is limited to the C89 standard, and the C99 standard has extended the length, in fact, most industrial standards are longer)

4. The identifier is case sensitive, that is, it is case sensitive. Generally, the variable name is in lower case, and the symbol constant name is in upper case.

5. The identifiers should be named "known and known", for example, length (Foreign Language: length), sum, total (Foreign Language: sum), circumference rate (Foreign Language: pi )......

6. in C language, identifiers are divided into three types: keywords, pre-defined identifiers, and user-defined identifiers.

 

 

Iii. Variables

 

Variables are only the amount that can be changed in the program. Once defined, three basic elements are available: variable name, variable type, and variable value.

 

1. variable name:

Naming rules for variables: http://www.cnblogs.com/onedime/archive/2012/11/21/2780149.html

 

2. Variable type:

In C language, variables follow the principle of "first defining and then using". The variables are generally defined in the form:

Variable type variable table name

The variable type is the data type stored in the Variable. For details about the data type concept, refer to the "C Language" data type and hybrid operation and type conversion in the previous blog.

(1) Integer Variables

① Store integer data in memory. In C, decimal, octal, and hexadecimal data can be used, but binary data is stored in the memory.

② The description of the basic type of the integer variable is int. Integer variables can be divided into the following categories based on the number of bytes occupied by memory:

Basic INTEGER: int, which occupies 2 bytes in a 16-bit system and 4 bytes in a 32-bit System

Short INTEGER: short int or short, which occupies 2 bytes in most computer systems

Long Integer: long int or long, usually 4 bytes

Integer variable type:

Signed short INTEGER: short [int] 2 byte-32768 ~ 32767

Unsigned short INTEGER: unsigned short [int] 2 byte 0 ~ 65535

Signed integer: int/signed [int] 4 byte-2147483648 ~ 2147483647

Unsigned integer: unsigned [int] 4 byte 0 ~ 4294967295

Signed long integer: long [int]/signed long [int] 4 byte-2147483648 ~ 2147483647

Signed long integer: unsigned long [int] 4 byte 0 ~ 4294967295

③ Integer Data overflow. In C, if the value of a variable exceeds the maximum value of its type, overflow may occur.

For specific data overflow tests, see the previous blog "C Language" Source code anti-Code complement and bitwise operations.

(2) real variables

① Store real data in the memory. Real Data occupies 4 bytes in memory and is stored in exponential form. The storage format is as follows:

Symbol bit + decimal part + exponent part

Note: All real data types are signed real data types, and there is no non-Signed real data type.

Ansi c does not specify the number of digits used in the memory to represent the fractional part and the exponent part. It is determined by the C compiling system.

② Common types of real variables provided by C language include float and double, and sometimes long double)

Real Variable type:

Single precision type: float 4 byte-3.4e38 ~ 3.4e38 6 ~ 7-bit valid bits

Double Precision: double 8 byte-1.7e308 ~ 1.7e308 15 ~ 16-bit valid bits

Long dual-precision type: long double 16 byte-3.4e4932 ~ 3.4e4932 18 ~ 19-bit valid digits

③ Rounding error of real data. Limited storage units provide limited valid numbers, so there will be computation Rounding Errors for real data.

Character variable

Character variables are used to store character constants. Each character occupies one byte of storage space. The type is char.

Character variable type:

Signed character char/signed char 1 byte-128 ~ 127

Unsigned char 1 byte 0 ~ 255

 

3. variable value:

In a program, a variable must be determined before it can be involved in various operations.

A variable can obtain a value through a value assignment statement or input statement, or an initialization method.

 

 

Appendix: ASCII code table

From http://tool.oschina.net/commons? Type = 4

 

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.