C language (ii)---constants and variables

Source: Internet
Author: User
Tags control characters

One, binary 1.1 binary 1.1 introduction

Binary data is a number represented by 0 and 12 digits. Its base is 2, the rounding rule is "every two in 1", Borrow rule is "borrow one when two". The current computer system is basically using binary systems.

1.2 Binary Data representation

Binary does not have 2 only 0 and 1, every 2 into 1, the order of the size of the weight of 22, 21, 2o. For example:
Binary data 0001 represents the decimal 1, the binary data 0010 represents the decimal 2,
Binary data 0011 represents the decimal 3, the binary data 0100 represents the decimal 4,
Binary data 0101 represents the decimal 5, the binary data 0110 represents the decimal 6,
Binary data 1000 represents the decimal 8, the binary data 1001 represents the Decimal 9,
Binary data 1010 represents the decimal 10, the binary data 1011 represents the decimal 11,
The binary data 1101 represents the decimal 13, and the binary data 1111 represents the decimal 15.

1.3 Characteristics of binary data

Adding a 0 to the tail of a binary number means that the number is multiplied by 2, as if the tail of the decimal is multiplied by a 0 for 10. For example:
Binary data 0001 represents the decimal 1, the binary data 0010 represents the decimal 2,
Binary data 0100 represents the decimal 4, the binary data 1000 represents the decimal 8,

Common multiples of 2:1 2 4 8 16 32 64 128 256 512 1024 2048 4096

1.2 Hex

English name: Hexadecimal, is a method of representation of data in a computer. is not the same as the decimal notation in our daily life. It consists of 0-9,a-f, letters are not case sensitive.

The corresponding relationship with the 10 binary is: 0-9 corresponds to 0-9; A-f corresponds to 10-15; The number of N-N-1 can be represented by a number of 0---(a) of more than 9 with the letter a-f. Binary only 0 and 1 No 2, decimal only 0-9 of the number is not 10, hexadecimal only 0-f, that is, 0 to 15 of the number is not 16.
At the end of a hexadecimal number, add a 0 to represent the number multiplied by 16, just as the tail of the decimal is multiplied by a 0 for 10. For example:
The decimal 32 is represented as 16 binary: 20
The hexadecimal 20 represents the decimal: 2x161+0x16o=32
The hexadecimal A5 represents the decimal: 10x161+5=165
The hexadecimal 100 represents the decimal: 16x16=256
The hexadecimal 23F represents the decimal: 2x16x16+3x16+15=575

Conversion of 1.3 binary

10 binary to 2: let the digit 2 take the remainder to be 1 or 0, divide the other bits by the corresponding multiples of 2

Second, constant

The C language constants include: Integer constants, floating-point constants, character constants, and string constants.

2.1 How to represent integer constants
    1. The decimal expression is the same as the normal number expression. For example: 13,28,-52,-1000, etc.
    2. A hexadecimal expression that starts with 0x. For example: 0xa1,0xc8,-0x20,-0x500, etc.
    3. An octal expression that begins with 0. Example: 032,-011

Note: The 0-beginning constant looks like a decimal, but it's not, it's octal, only 0-7 digits in octal, 8 and 9 are error codes.

Note: The C language does not provide a binary constant expression.

2.2 Floating-point constants

Floating-point constant, popularly speaking is the number with a decimal point, the expression must contain a decimal point, even if the fractional part is 0, also the default with decimal points.

There are two main types of expressions:

    1. Single-precision floating-point number: The floating-point number with the trailing F represents a single-precision floating-point number. For example: 0.5f,88.2f,-55.0f and so on.
    2. Double-precision floating-point number: A floating-point number with no f at the tail represents a double-precision floating-point number. For example: 0.88,99.5,-111.356 and so on.

It is generally considered that the double-precision floating-point number is much larger than that of single-precision floating-point number, including integer part and fractional part, and double-precision expression is much larger.

2.3 Character Constants

Character constants are a single quotation mark that encloses a character, each enclosing a different character representing a different number (as if each student had a different name and a different school number).

The comparison of all characters and values is the ASCII encoding table, and the ASCII encoding used as a character constant is mainly used for numbers in the range of 0-127. Of these, 0~31 and 127 (total 33) are control characters or communication-specific characters, and the remainder are display characters.

For example: char c = ' A ';
' A ' represents a number 65 because the value of a in the ASCII-encoded table is 65, and so on:

' B ' represents the 97, ' a ' represents
' X ' stands for 88, ' 5 ' for 53,
' = ' stands for 61, ' | ' Rep 124,

The ' space ' stands for 32 and so on.
As a result, character constants are actually an integer constant, except that the range of character constants is relatively small.
All ASCII codes can be represented by the "\" plus number (2-bit 16-digit or 3-bit 8-digit).
In the C language, some of the letters used in front of the "\" to denote common ones that do not display ASCII characters, such characters are called escape characters. For example: char c = ' \ n ', where ' \ n ' stands for 10, and is printed out to represent the newline "new line". And so on: ' t ' stands for 9, and is printed out to represent a tab distance "Table".
' 0 ' represents the end of the string when printed out.
' \ \ ' stands for 92, which is a backslash when printed out.
Note that you want to print a backslash in the C language, which must be represented by ' \ \ ' in character constants.

' \ ' represents 34, and the printout represents a double quote.

Char c= ' \x25 '; Represents 37 of the printed output is%, Char c= ' \053 '; Represents 43 of the print output is +.

2.4 String Constants

String constants are pairs of character sets enclosed in double quotation marks.

For example, the following is a valid string constant: "How does You do.", "China", "a", "$123.45",
Storage: Characters in a string are stored sequentially within a contiguous area of memory, and the null character ' \ s ' is automatically appended to the end of the string as the ending flag of the string. Therefore, the number of characters N of the string in memory should occupy (n+1) bytes.
You can use printf to output strings, for example: printf ("How does You do.");
Comparison with character constants:

    1. Character constants are single characters that are enclosed by a pair of single quotes, and string constants are pairs of character sets enclosed in double quotation marks.
    2. Character constants are essentially a number, which corresponds to a character, whereas a string constant is essentially a memory address of a character set, which is printed from the head to the end of the end.
    3. You cannot confuse a string with a character constant. Character constants can be assigned to a char variable, but you cannot assign a string constant to a character variable, and you cannot assign a value to a string constant! For example: Char b= ' a '; (correct) char b= "a"; (error)
Three, the concept of variable 3.1 bytes

The computer takes a byte (byte) as a unit of measurement of the storage capacity and transmission capacity, and one byte equals the 8-bit binary number. The number of bits in memory can be stored in a value of only 0 and 12, and two bits can be stored in 00, 01, 10, and 11. These four values represent 0, 1, 2, 3 respectively. And so on, if there are 4 bits of space to store the number is: 0-15 (0x0 to 0xF).

  

Thus, the memory space on the number of n can be stored is 2 of the n-th square.
A byte represents 8 bits of memory space, the value that can be stored is 2 8, that is, 0 to 255, the hexadecimal expression is 0x00 to 0xFF value

3.2 Classification of variables

The C language variable refers to a memory space for accessing data.

    • According to the space length of the variable can be divided into:
      • Character variable (1 bytes): char type, unsigned char type;
      • Short Variant (2 bytes): Shorter type, unsigned type;
      • Long variable (4 bytes): int, unsigned int (or long, unsigned long) or float type, pointer variable, and so on.
      • Super-long variable (8 bytes):d ouble type, __int64, and so on.
    • Depending on the type can be divided into:
      • Integer variables: include char, unsigned char, short, unsigned short, int, unsigned int (or long, unsigned long), and __int64.
      • Floating-point type variable: float type (single precision), double type (dual precision);
      • Other variable types are: pointer variables: array variables, structure objects, etc.;
3.3 Storage range of variables

From the table above, we can see that the 4 digits are exactly the single digits of the hexadecimal digits, and if you add 4 digits, 8 bits are exactly the hexadecimal two digits. So the computer with 8 bits represents a byte, the number expressed is (0-255) 0x00 to 0xFF a total of 256 digits, just as the two digits in decimal are 0 to 99 total 100.

3.3.1 Single-byte variable (8-bit)

Single-byte variable (8-bit): unsigned char and Char

  

unsigned char: Unsigned single-byte variable with a storage range of 0-255 (0xFF).

Char: A signed single-byte variable with a storage range of 128 to 0 to 127.

In order for the Char type to store 256 numbers, half of which are positive and half negative, the computer takes the highest bit in the Char type variable as the symbol bit. The highest bit is 0 positive, and the highest bit is a negative number of 1.

In a negative interval, subtracting the value of unsigned char by 256 is the char type.

  

  

3.3.2 Double-byte variable (16-bit) unsigned short and short

  

Unsigned short: unsigned double-byte variable that stores a total of 65,536 0-65535 (0xFFFF), just like the 4 digits in decimal is 0 to 9999 total 10,000

Short: A signed double-byte variable with a storage range of 32768 to 0 to 32767.

In order for the 65536 numbers stored in the short type to be half positive and half negative, the computer takes the highest bit in the short type variable as the symbol bit. The highest digit is a positive number of 0, and the highest bit is 1 for negative numbers.

In a negative range, subtracting the value of unsigned short by 65536 is the value of the short type.

  

3.3.34-byte variable (32-bit): unsigned int and int (or unsigned long and long)

unsigned int: Unsigned four-byte variable that stores a total of 4,294,967,296 0-4294967295 (0xFFFFFFFF), just as 8 digits in decimal are 0 to 99999999 for a total of 100 million

INT: Signed four-byte variable with a storage range of 2147483648 to 0 to 2147483647.

In the computer: 1k=1024,1m=1024k,1g=1024m.
65536 is exactly divisible by 1024 equals 64, so 65536 is also called 64k,32768 called 32K; 4294967296 is exactly divisible by 3 1024, so also known as 4g,2147483648 is called 2G.

In order for the int type to store 4G numbers, where half is positive and half is negative, the computer takes the highest bit in the INT type variable as the symbol bit. The highest digit is a positive number of 0, and the highest bit is 1 for negative numbers.

In a negative range, subtracting the value of unsigned int by 4G is the numeric value of type int.

   

3.3.4 Other types of variables

Pointer variables: 32-bit operating system is also 32-bit, storage range is between 0-0xffffffff.
Floating-point type: the storage structure of floating-point variables is completely different from the storage structure of integer variables.
The fractional portion of the float type can be up to 6 to 7 bits long, and the integer growth occupies a fractional portion.
The fractional part of a double type must be at least 10 digits above the decimal point, and the increase in the integer number will affect the precision of the decimal part.

C language (ii)---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.