Aaronyang C language [2] [basic types, operators, and expressions]

Source: Internet
Author: User
Tags binary to decimal bitwise operators comparison table

The onyang style is: do not talk about things that grow up, but explain things that are hard to understand in the topic. Other things have been summarized and discussed. You can use them as examples to enrich the content of the article.

This article has many understanding aspects, but I have explained it with GIF images (my own Photoshop ).

I don't know much about underlying things. It's impossible to give it out.

This article is intended for those who have learned other programming languages. I skipped a few lectures in many places, so this is not an entry-level article.

When I was reading C, I found some interesting things, such as macro replacement, signed, and unsigned types. The constant definitions of C are also different.

Focuses on bitwise operators and comma operators.

The most important thing is how many bytes each type occupies, which can be used by optimization programs in the future.

Avoid unnecessary type conversion

 

Highlights preview:

For example, many people know how to write programming, but they know little about the principle and how to use it. For example, the differences in programming between 32-bit and 64-bit operating systems

It is easy to understand here. I have made some pictures for easy understanding. If I want to see them directly, I will refer to the last section at the end of this article.

 

Overview of Data Types

I learned C #, but I have never touched on the unsigned type and pointer.

Other concepts:

When the program is opened, the data will exist in the memory (some may be in registers). We need to allocate several bytes to each data in the memory to store the data. The memory bytes occupied by the data are called the "Data Length" of the data ". Therefore, we need to define different data types to arrange proper lengths for data storage allocation. So we need the concept of data type. I don't know if I understand it.

Constant
(1) Integer constants
  • Decimal: Common number Writing Method, 0, 22, + 11,-3
  • Octal: the writing format starting with 0, for example, 00, + 08,-033,021
  • Hexadecimal: the writing format starting with 0x, for example, 0x1,-0x121, + 0x12, 0x21.

Bytes occupied: Generally, the microcomputer occupies 2 bytes, regardless of the operating system. Their value ranges from-32768 to decimal ~ + 32767

Long integer constant: range:-2147483648 ~ + 2147483647, occupying 4 bytes.

Writing format: add L or L at the end of the integer, for example, 10l-011l + 0x15l

The rest are short Integer constants. If an integer constant is not followed by the letter "L" or "L" and exceeds the value range that can be expressed by a short integer constant, the constant is automatically considered to be a long integer constant, for example,-32769, 32768, and 40000 are Long Integer constants.

 

Summary: Integer constants are classified into two types: short and long. They are also divided into three types: decimal, octal, and hexadecimal. When using them, you must differentiate them. For example, 88 and 88l are the same value, but they occupy different numbers of bytes in the memory. For example, 10,010 L and 0x10 are short Integer constants, But they indicate different integer values.

 

 

(2) general form of real constants

[+ Or-] integer part decimal part

Exponential form

[+ Or-] integer part decimal point decimal part E or E [+ or-] index (which is a common integer)

 

For example, 12.345e3 is equal to 12.345 multiplied by 10, and the power of 3 is 12345.

12345e-3 is equal to 12345 multiplied by 10 to the power of-3 is 12.345

 

Summary: a real constant occupies 4 bytes in a general microcomputer. The value ranges from-10 to 38 to 10, and the valid number is 7 digits.

For example, 1.23456789 and 1.234567 are the same. Because the valid number is 7 digits, the last two digits are invalid.

 

(3) character constants

It is a char type. In C, letters are case-sensitive, and 'A' are different.

In memory, each character constant occupies one byte and stores the ASCII code value corresponding to the character. For example, 'A' a' 1' % ''\ r'' \ X3D 'is stored in the memory in bytes as a decimal Integer 97 65 49 37 13 61.

Conclusion: Because Integer constants store integer values in the memory, if the value ranges from 0 ~ Between 127, the C language can also be regarded as a numeric constant, that is, the number is used as a symbol. For example, Integer constants 111, 70, and 40 can be used as character constants 'O' f ''('

There is a good example: 'A' + 5 here, a will be used as 97, so it is equal to the result value of 102. I also tried it in C #.

 

(4) string constants

It is a string defined in C #.

But the string defined in C is not a string. Let's talk about it later.

For example, "aabbccdd" Escape Character \

String Length

Let's just say something special. For example, if "" is 0, "\ ABCD \" is 6, and "\ 101 \ 102 \ x43 \ x44" is 4.

Although each character occupies only one byte in the memory, the C language requires that the number of bytes occupied by each character string in the memory is equal to the length of the string + 1, the character stored in the last byte is an "empty string" with a value of 0. The Escape Character "\ 0" is often used for writing. It is called the end mark of a string in C language. For example, the length of the string "AB" and "A" is 2 and 1 respectively, but they occupy 3 and 2 bytes in the memory, therefore, "a" and 'A' are different. One occupies 2 bytes and the other occupies 1 byte.

I don't know whether this is true in C...

 

(5) Symbolic Constants # Define symbol constant

The C compiler automatically replaces all symbolic constants with their corresponding constants before compiling the program.

The variable name must contain uppercase letters.

For example, # define PI 3.1415926 achieves the same effect as C # const double Pi = 3.1415926

# Define a 'A' is equivalent to the const char a = 'a' of C'

 

The general format of macro definition commands is as follows: # define macro name is a string of symbols

The macro name is equivalent to the variable name. You can think of it as the corresponding value.

Macro replacement is to replace the name with the value and then process it. Since macro replacement is performed before compilation, macro definition commands belong to the "pre-compiled commands" in C language"

 

Basic example: Understanding the replacement process

# Define PI 3.14159

# Define R 10

# Define D (R + r)

# Define l pI * d

If l is used in the program

① Replace it with pI * D first

② Replace PI with 3.14156 and D with (R + r), and the result is 3.14159 * (R + r)

③ Here, there is a macro name, and then replace R. The final result is 3.14159*(10 + 10)

 

The following is a special example:

Example: # define PI 3.14159

# Define R1 2 + 3

# Define R2 (2 + 3)

If the macro value of 2 * pI * R1 is replaced with "2*3.14159*2 + 3"

If the macro value of 2 * pI * R2 is replaced with "2*3.14159*(2 + 3 )"

 

Variable
The definition mode is basically the same as that of C #.

Type variable name = Value

Type variable name

Let's take an example.

Int I;

Float I, J;

Unsigned short me;

Long a1 = 11l;

Unsigned short a1 = 0111, a2 = 0x11;

Int I = 1, j = 2, K;

Int I, j, k = 10;

Char C1 = 'A', C2, C3 = 'a ';

 

Definition of a famous constant (const in C)

The variable specification is the same in C # defined by the const type name, and the name is all uppercase letters.

C:

Const data type operator variable name 1 = initial value, variable name 2 = initial value 2;

Same as C # const

For example, const char Fale = 'male' and female = 'female ';

Once defined, the program cannot be modified while running; otherwise, an error is reported.

 

Operator (similar to C #, there are several more operators. Here we assume you have learned other programming languages)

Operators are the most basic

Add, subtract, divide, and calculate the remainder +-*/%

Plus or minus sign +-

Relational operators <><=>==! =

There is! & |

Ternary operators (the three elements in the book, binary are three eyes, two eyes, I am not used )? :

Assign values to other symbols = comma, and [] other basic brackets () in the array increase progressively. ++ --

+ =-= * =/= % =

Sizeof, which is not in C #

For member operators, in C #, all the attributes or methods in the object are obtained by the object vertex (.). In C, use-> and. About-> in PHP.

Pointer operator, which should not exist in C # (& * + -)

Bit operators that are hard to understand (for example, <> ^ ~ & | & =, Etc.). In fact, C # is also available. We rarely use it. I will explain it later.

 

There is also the concept of associativity related to computing, the concept of computing priority.

As I have mentioned in Javascript before, the Language Logic is universal. For more information, click here.

 

I. Skip

Ii. Length operator (sizeof)

Open C-Free5

Sizeof is the keyword in C. It is not a function. It means that the memory occupied by C is measured in bytes.
Getch (); you can press any key to exit when running this command. If this command is not available, the screen will disappear (the program will exit after it ends) and the result will not be visible.

Equivalent to console. Readline () in C ();

 

 

 

Bit Operators

In programming: 0 represents false, false, 1 represents true, true, there is a phrase, not 0 is true

 

I. bitwise logical operators

Combination of number and location object type result types

Bit non ~ Single Object prefix Integer type from right to left

Bitwise AND & binocular incubation integer from left to right

Bit or | the left-to-right side of an integer in the binocular fix

Add By bit ^ The left-to-right integer in the binocular fix

 

Comparison table for hexadecimal to binary

[Example 1]

The variable is defined as follows: Unsigned short a = 0111, B = 0x53

First convert a unsigned octal to binary: 0000 0000 0100 1001

B unsigned hexadecimal to binary: 0000 0000 0101 0011

① ~ A converts the position where the binary value is 0 to 1, and the value of 1 to 0 (the corresponding binary value is 1111 1111 1011 0110), and converts it to 0177666 in octal value. After the operation, a remains unchanged.

② A & B, two-digit pairs correspond, with 0 and 0, and the corresponding binary value is (0000 0000 0100 0001), and then converted to octal value 0101. After calculation, a remains unchanged.

③ A | B, which corresponds to two bits. There is 1 for 1, and the corresponding binary value is (0000 0000 0101 1011). Then it is converted to 0133 for the octal value. After calculation, a remains unchanged.

④ A ^ B, which corresponds to two bits. If the difference is 1, the corresponding binary value is (0000 0000 0001 1010) and then converted to octal value 032. After the operation, a remains unchanged.

 

 

Ii. offset Operators

Object Type result type associativity using number and location calculation rules

Left shift <binocular fix a <B. A shifts left to the right of the integer type B from left to right

Right Shift> binocular fix a> B. A shifted to right shift from left to right

During Shift, all the removed digits are discarded. The number of removed vacancies is related to the Left shift or the right shift.

If it is left shift, it is required that all the numbers to be filled are 0; if it is right shift, if it is not signed, all the numbers to be filled are 0; if it is signed, then all the supplemented numbers are equal to the leftmost bits of the original number (that is, the symbol bits of the original number)

 

The following assumes that a has been converted to binary. Originally a = 47, it is now converted to binary 101111.

Unsigned shift left, move left with a symbol. The two of them are the same. I am doing an image demonstration.

 

Unsigned right shift

Shift right with symbol
Shift to the right with the symbol (0 on the leftmost side, 0 on the left side, 1 on the left side, and 1 on the left side)

After moving, convert the binary to decimal. This is the offset operator.

 

 

Iii. bitwise auto-inverse value assignment operator

The function is similar to commonly used + =-=. After calculation, the original value is changed.

Mainly the five: <=>=

I will not talk about it here, that is, after the displacement operation, we will change the original value. The first part and the second part will focus on the operation without changing the original value.

 

 

 

 

 

Expression (same as C # 90%)
  1. Arithmetic expression
  2. Relational Expression
  3. Logical expression
  4. Value assignment expression
  5. Comma expression
  6. Conditional expressions

The special usage of commas not found in C # can constitute a comma expression.

In addition to int A, B = 10 when declaring variables, comma is also used to separate function parameters.

However, none of the commas are operators. In C, commas can be an operator. Let's take a few examples.

Comma operator, which can be used to connect two expressions. For example:

3 + 5, 6 + 8

It is also known as a comma expression and an ordered value operator ". The comma expression is generally in the form

Expression 1, expression, expression 3 ...... Expression n

The final result is based on the value of the last expression, that is, the value of expression n.

For example, the value of the preceding comma expression is 6 + 8, that is, 14.

 

Example 1

Here, 3 is multiplied by 5 to 15, and then the value is assigned to a, a is equal to 15, and then 15 is multiplied by 4. The value of the entire expression is 60.

Example 2

Strong expression ability in C language. One important aspect is its rich expression types and strong operator functions. Therefore, C is flexible and adaptable.

 

Small Example of type conversion, reminder optimization (variable = expression)

C # has both forced conversion and implicit conversion, and C also has automatic conversion rules and forced conversion rules, which are the same as C # 99%.

  1. Automatic conversion rules for data types in expression calculation: all data involved in the calculation is converted to the data type with the longest data length, and then calculated ----- "the length is not short".

    For example:

  2. Automatic conversion rules for data types when the calculation result is stored in the variable: first, the Data Type of the calculation result is automatically converted to the Data Type of the Left variable, then assign the variable ----- "so that the Left is not right".

    For example:

  3. Mandatory data type conversion rules for calculation results: Same as C #, for example, (INT) 14.5, that is, 14

    For example:

  4. Small examples to avoid conversion

    For example:

Extra-curricular knowledge

One byte is equal to eight binary bits, that is, 1 byte = 8bit.

The computer uses binary, 8 = 2 ^ 3, usually the highest bit is the symbol bit

In a binary system, each 0 or 1 is a bit, which is the minimum unit of data storage. 8 bits are called a byte ). The number of CPU bits in a computer indicates the maximum number of CPU bits that can be processed at a time. For example, the CPU of a 32-bit computer can process up to 32-bit data at a time.
Bit, short for binary digit (Binary Number) digits, is the term proposed by the mathematician John Wilder Tukey (may have been proposed in 1946, but it was said in 1943 ). This term was used for the first time in Shannon's famous "Information Theory" (a mathematical theory of communication ).

Therefore, the 64-bit Operating System reads more digits each time, so data processing is faster, but there may be compatibility issues. Here is a small question.

Recall the first part of this article

Short-type byte length 2

The length of the long type is 4.

The following is an illustration.

The variable B3 is 4 bytes. After storing the variable 65536, the corresponding binary digits are as follows:

When the value of B3 is assigned to variable a, a new value consisting of 4-Byte Low 16-bit data (two byte on the right) of variable B3 is assigned to variable A, and is finally converted to decimal display, is 0

 
 
Learning Summary
  1. Understanding and using constants, macro replacement, and variable principles
  2. Understanding of Data Length and hexadecimal conversion
  3. Understanding and use of bitwise operators
  4. Use of the comma Operator
  5. Understanding and practice of the number of bytes occupied
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.