01_C language basics, 01_c Language

Source: Internet
Author: User
Tags bitwise operators

01_C language basics, 01_c Language
Abstract:

1. C language Overview
2. Data Types, operators, and expressions
3. C language program structure

4. VC6.0 exercise

Knowledge explanation 01: C Language History

1. Comparison between C language and other languages

Assembly language:

(1) The hardware can be operated directly, and the execution efficiency is relatively high. It depends on computer hardware and has poor readability and portability.

(2) perform operations on the hardware. The execution efficiency is high. A string of digital code is used to represent commands, which is closer to the detailed operation steps used by the computer. Relying on computer hardware (for example, writing an assembly program to control the IO of 51 single-chip microcomputer, but this program may not play a role in the AVR), it is quite difficult to view the assembly.

General advanced languages: C ++, java, C #, etc.

(1) the C language syntax and basic structure are extended and developed;

(2) Poor hardware operation support and low operating efficiency

2. C language was invented by Bell Laboratory in early 1970s. In early 1980s, the American National Standardization Association (ANSI) developed the ansi c standard.
3. subsequent extensions in various fields: vc in windows and gnu c in Linux

Note: The C language is platform-based. If only the C language is used, nothing can be done.

 

Knowledge 02: C Language Features

1. In summary, the C language has the dual features of both assembly language and advanced language;

Dual Nature means high efficiency and good readability.

2. Specifically, the main features of the C language are as follows:

(1 ). the language is concise, compact, easy to use, flexible, 32 keywords, 9 control statements (control program flow ), the program form is free (one thing can be implemented in multiple ways (such as loop )).
(2) rich operators (convenient program design) and 34 operators.
(3) rich data types: integer, real, struct, array, pointer, struct, and shared body.
(4). Structured control statements, such as (if .. else, while, switch, etc.) are fully modular and structured languages.
(5). modularization: a way to break down complicated systems into better-understood and manageable modules
(6). Structuring: Order a complex solution process to make each piece easier to understand
(7). allow direct access to the physical address, perform bit operations, and directly perform hardware operations.
(8). The generated target code is of high quality and high execution efficiency. It is only 10%-20% less efficient than the target code generated by the assembler.
(9). Good portability and can be used in various types of computers and operating systems without modification.

(10 ). good combination with linux (the core of Ios, windows, and UNIX is implemented in C Language). Linux and UNIX are written in C language. in Linux, C language development is very efficient, especially in the embedded field. C language is the first choice for development.

Knowledge description 02: keywords

1. C has 32 keywords

Data Type keywords (12)

char, short, int, long, float, double, unsigned, signed, struct, union, void, enum

Unsigned, signed: modifier, int a = 125; default value: signed

Void: it cannot be used to define variables, but can be used to modify various variables.

Control statement keywords (12)

If, else, for, do, while, break, case, continue, default, goto (do not use), return, switch

Storage keywords (4) (important introduction, because the previous knowledge points will be described later)

Auto, extern, register, static
Extern: can be placed before a variable or function to indicate that the definition of a variable or function is in another file. It can be used externally.

Register: modifier implies that the variables of the Compilation Program will be frequently used. If possible, store the variables in the CPU registers to speed up storage.

The register variable must be of the type that can be accepted by the CPU. This usually means that the register variable must be a single value and the length should be less than or equal to the length of an integer. However, some machine registers can also store floating point numbers.
Because the register variable may not be stored in the memory, you cannot use "&" to obtain the address of the register variable.

Static:

All static variables are allocated memory in the global data zone.

Static global variables are visible in the entire file declared, but invisible outside the file;

Other keywords (4)

Const, sizeof, typedef, volatile

Volatile:

Make sure that this instruction is not omitted due to compiler optimization and requires that values be read directly each time. Simply put, it is used to prevent the compiler from optimizing the Code, for example, the following program:

Num = 0x55;

  Num = 0x56;

  Num = 0x57;

  Num = 0x58;

For external hardware, the preceding four statements indicate different operations, which produce four different actions, however, the compiler cannot optimize the preceding four statements just like a pure program. It only considers num = 0x58 (ignore the first three statements and generate only one machine code ). If you type volatile, the compiler will compile one by one and generate the corresponding machine code (four)

 

 

Knowledge description 03: Statements

1. C language uses executable statements in the function body to send operation instructions to the computer system. According to the function or composition of statements, C language statements can be divided into five categories:

2. control statements (9, which can be subdivided into three types)

Select structure control statement

if()~else~,switch()~

Loop Structure Control statement

for()~,while()~, do~while(),break, continue

Other control statements

Return, goto

Goto

Return function:

Terminate the execution function and assign values (optional)

Returns the control to the next statement that calls the function.

Where is the return value of the function?

For the four-byte return value, the compiler generally puts it into the eax register, and the return value greater than 4 bytes is generally placed before the address is returned in the stack. Certainly, it should not be in the heap.

For example:

3. function call statement

The function call statement is composed of a semicolon (;) for the callback function call.

For example:

Printf ("hello world !");

Scanf ("% d", & I );

4. expression statements

An expression statement is composed of a semicolon after the expression,

For example:

"Num = 5" is an expression,

"Num = 5;" is the value assignment language;

5. empty statement

An empty statement consists of a semicolon. No operation is performed on an empty statement.

6. Compound statements

Composite statements are composed of a group of statements enclosed by braces.

Main ()

{

{} // Compound statement. Note that no score is required after braces;

}

Compound statements are generally used in combination with other statements:

For example, (;;){}

 

Knowledge details 04: Overall Structure

 

1. in C language, in addition to nine control statements, including the Implementation sequence, selection, and loop, input and output operations are implemented by standard library functions (not part of C language.
2. A complete C program is composed of one and only one main () function (also called the main function) and several other functions, or only one main () function () function structure.
3. All functions can be stored in the same source file or in different source files.

Knowledge details 05: C file structure

 

1. a c-based software system generally consists of the following files:

 

 

Several C files: each C file contains several functions.

Several header files: Each header file contains some data structure definitions and prototype declarations of C functions and variables.

Several library files: the library files are compiled binary files, which are generally composed of several C files, including executable code of several functions; the executable code in these library files is merged into the final executable file during connection.

Several make files: The make file describes the dependencies among multiple files and the information required to generate the final executable file or library file.

 

 

Knowledge description 06: Data Types

1. Data Type

 

 

 

2. constants and variables

 

Constant: the amount of data whose value cannot be changed while the program is running (Note: it cannot be reversed)

 

Integer: 100,125,-100,

Real-type 3.14, 0.125,-3.789

Struct type 'A', 'B', '2'

String "a", "AB", "1232"

Variable: its value can be changed. The variable name can only consist of letters, numbers, and underscores. The first word must be a letter or underscore. The variable is allocated with the corresponding memory address during compilation.

A variable is actually a "box" used to hold values and other content"

 

3. Integer Data

 

The expression of an integer constant is in decimal format. The octal value starts with 0, for example, 0123. The hexadecimal value starts with 0x, for example, 0x1e.

Integer Variables are divided

 

Unsigned basic integer (un/signed) int

Signed short INTEGER (un/signed) short (int)

Signed/unsigned long integer (un/signed) long (int)

Integer constant in c99: In c99, the integer constant ending with LL Or ll (both uppercase and lowercase letters must be consistent) is long int.

Note: in the school's TC environment, int Is two bytes, because TC actually simulates a 16-bit environment on a PC, it is 4 bytes in 32-bit environments such as VC and Linux. It can be obtained using the sizeof (type | variable) operator.

 

4. Real Data

 

Real Constants

 

Real type is also called floating point type, and real type constants are also called real numbers or floating point numbers.

Decimal format: composed of digits and decimal points: 0.0, 0.12, 5.0

Exponential form: 123e3 represents the 3rd power of 123*10

Real Variables

Float and double)

Float: 4 bytes, 7-digit valid number (decimal)

Double Type: 8 bytes, 15 ~ 16-digit valid number (decimal)

The constant that contains the decimal point but does not end with "f" is double.

The decimal point ends with "f ".

 

5. character data

 

Character constants: enclosed in single quotes, such as 'A' and 'B.

 

Escape characters: Start with a backslash "\", followed by one or more characters, such as '\ n' \ t', which respectively represent line breaks and horizontal escape.

 

Use the escape sequence of octal and hexadecimal numbers in the string literal. The escape sequence of the octal number ends after three digits, or at the first non-octal digit. For example, the string "\ 1234" contains two characters (\ 123 and 4), while the string "\ 189" contains three characters (\ 1, 8, and 9 ). The escape sequence of the hexadecimal number is not limited to three numbers, but ends with the first non-hexadecimal number.

 

Instance 1:

 

 

Instance 2:

 

 

Instance 3:

 

 

Most compilers report the preceding errors, because the escape sequence of hexadecimal numbers is usually limited to \ x0 ~ \ Xff

 

Character variable:

 

Variables defined by char can only store one character constant;

Each character variable is allocated a byte of memory space. character values are stored in the memory unit of the variable in the form of ASCII codes.

Note:

A = 'X ';

The a variable contains the ASCII character 'X': 120

That is, a = 120 is essentially the same as a = 'x '.

 

6. string constants

 

It is a character sequence enclosed by double quotation marks. For example, "CHINA", "C program", and "$12.5" are all valid string constants.

 

7. Differences between string constants and character constants

 

 

Example 1:

Example 2:

The above example shows that a and B are equivalent. Whoever is equivalent will check whether the values in their memory are equal.

 

8. Symbolic Constants

 

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

# Define identifier constant

Where # define is a preprocessing command called macro definition command. Its function is to define this identifier as its constant value.

Example: Calculation of each data type: (Path: \ student version \ 01_C language basics \ 00 _ basic code \ limit 0

 

9. boolean type in C99

 

Bool flag; // defines a bool variable, which must contain the header file # include <stdbool. h>

10. There are different types of data. The mixed operation between different types of data will inevitably involve the type conversion problem. There are two conversion methods.

Automatic conversion:

The compilation system automatically completes the process according to certain rules.

Forced type conversion:

Convert the operation result of an expression to the required data type.

Regardless of the conversion, it tries its best to ensure that data is not lost.

 

11. Principles of automatic conversion

 

Conversion direction:

Description

 

Horizontal arrow, indicating the required conversion (problematic); char and short must be converted to int type

Data type conversion rules: the type that occupies less memory bytes (with a small value range) and converts to a type that occupies more memory bytes (with a large value range) to ensure accuracy is not reduced

Question: signed ---> unsigned

 

12. Forced conversion: implemented through type conversion

 

(Type specifier) (expression)

Function: forcibly convert the operation result of an expression to the type indicated by the type specifier. For example:

 

(Float) a; // converts the value of a to a real type.

(Int) (x + y); // converts the result value of x + y to an integer.

Note:

The type specifiers and expressions must be enclosed in parentheses;

Both force conversion and automatic conversion are temporary conversions of the Data Length of the variable for the purpose of this operation, without changing the data definition type.

Example 1-type unchanged:

Example 2-the value does not change:

 

 

Knowledge explanation 07: operators and expressions

1. operators used for data calculation include:

 

 

Types, priorities, and associativity of Operators

 

2. Types of Operators

 

There are a large number of operators and expressions in C language, which are rare in advanced languages. It is precisely because of the rich operators and expressions that make C language function very perfect.

 

3. Common operators in C Language

 

Arithmetic Operators (+,-, *,/, %)

Relational operators (>,<, ==, >=, <= ,! =)

Logical operators (! , &, |)

Bitwise operators (<,>, &, | ,~ , Region)

Value assignment operator (=, and its extended value assignment operator)

Conditional operators (? :)

Comma operator (,)

Pointer operators (* and &)

Sizeof ())

Forced type conversion operator (type ))

Component operator (.->)

Subscript operator ([])

Others (such as function call operators ())

Example:

The equal sign is higher than the comma priority.

 

4. A formula that connects an operation object (also called an operand) using Arithmetic Operators and complies with the C syntax rules. It is called a C arithmetic expression. calculation objects include constants, variables, and functions. (A function refers to the return value of a function)

 

Example: a * B/c-1.5 + 'A ′

 

5. compound assignment operators

 

Add other binary operators before the value assignment operator "=" to form a composite value assignment operator: + =,-=, * =, % =, <=, >>=, & =, ^ =

 

A + = 5 // equivalent to a = a + 5;

X * = y + 7 // equivalent to x = x * (y + 7)

6. Auto-increment and auto-subtraction Operators

The function is to increase or decrease the value of a variable by 1.

++ I, -- I (Add/subtract first, then use)

I ++, I -- (first use, then add/subtract)

Example 1

Example 2:

Example 3:


7. When determining the order in which operators of the same priority are calculated, pay attention to associativity. For detailed precedence and associativity, see the following table.
8. Priority

 

 

 

Knowledge explanation 08: C language program structure

1. c supports the three most basic program running Structures

 

Sequential structure, selection structure, and cyclic structure

2. Ordered Structure: The program is executed in order without jump.
3. Select structure: Execute corresponding functions based on whether the conditions are met.

If (expression) statement;

 

If (expression) Statement 1 else Statement 2;

 

If (expression 1) Statement 1;

Else if (expression 2) Statement 2;

...

Else statement n

 

NOTE: if... else, this can only respond to one condition

Example

Example 1: Determine whether a number can be divided by two. (Exercise if)

Example 2: Determine the remainder of a number by two. (Exercise if {} else {})

Example 3: Determine the remainder of a number by 3. (Exercise if {} else if {})

4. Conditional Operators

(A> B )? A: B;

Note: If the condition is true, the expression value is a. Otherwise, the expression value is B.

The operation Priority of conditional operators is lower than that of Relational operators and arithmetic operators, but higher than the value assignment operator. max = (a> B )? A: B. can I remove the parentheses and write them as max = a> B? A: B;

? And: are a pair of operators and cannot be used separately.

The combination direction of conditional operators is from right to left.

Concise, but error-prone, generally not recommended

The expression cannot be a floating point or string.

5. Loop Structure

Goto statement (use as few as possible ).

The while statement first judges the expression and then executes it.

The do-while statement first executes the statement and then judges the expression.

For (;;){}

For statement in C99

The first expression in the for statement can be used to define a variable. This variable can only be used in a loop.

Gcc Compilation

To solve this problem, you only need to add-std = c99 during compilation.

6. What is the difference between a break statement and a continue statement?

The break statement is used to exit the current loop.

Continue is used to end this loop

 

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.