Chapter 2 C language structural data type keywords

1. The structure of C language

1. Procedure

A program consists of one or more source program files

2. Source files

  1. preprocessing directives
  2. global declaration
  3. function definition

3. Function

Functions are an important part of the C language.
A C language program is composed of one or more functions, which must contain and can only have one main function.

1. Function header: that is, the first line of the function
int max (int x, int y).
If there are no parameters, you can write void or empty
2. Function body:
The outermost curly brace {} is the scope of the function body.

  1. Declare the definition of some variables
  2. Perform operations performed in some functions

It can be an empty function, with no declaration part and no execution part.

2. The running process of C language

Edit->Compile->Connect->Execute

Compile:

1. Precompile.
Expand the header file in .c and macro expand the .i file generated by
gcc -E ac -o ai . 2. Compile the preprocessed .i file to generate a .s assembly file gcc -S ai - o as 3. Assembly Generate .o target file from .s assembly file gcc -c as -o a.so






preprocessing

include

#include<>Use <> to include the header file and search for the header file in the path specified by the system.
#inlcude "" Use "" to include the header file. Now look for the header file in the current directory. If it is not found, look for it in the path specified by the system.

Note: No syntax checking is performed at this stage.

define

To define a macro, use define.
Macros are replaced during precompilation.
Scope: from the beginning of the definition to the end of the file, or the end of #undef.

1. Without parameter macro

#defind PI 3.14
When pre-compiled, if PI is used in all places in the code, replace it with 3.14.
Benefit: Just modify your definition of macro i and it will be replaced everywhere in the code.
#undef PI macro definition scope ends

2. Macro with parameters

#define S(a,b) a*b

S(1,2) => 1*2;
S(1+2, 3) => 1+2*3;

selective compilation

1、#ifdef #else #endif

//如果定义过宏NUM,就执行片段1,否则片段2
#ifdef NUM
...
#else
...
#endif

2、#ifndef #else #endif

Contrary to the first one

3、#if #else #endif

//1为真,则执行1,如果写成#if 0.0为假,执行2
#if 1
//代码块1
#else
//2
#endif

3. Data type

Insert image description here
The values ​​of basic type and enumeration type variables are numerical values ​​and are collectively called arithmetic types.
Arithmetic types and pointer types are collectively called scalar types.
Array types and structural question types are collectively called combination types.
A collection of several data of the same or different types is called a constructed type

3.1. Integer data

Starting with 0 means octal.
Starting with 0x means hexadecimal.

3.1.1、int

4 bytes under 32-bit systems and 2 bytes under 16-bit systems. Determined by the build system.
Storage method : stored in the complement form of an integer.
The two's complement of a positive number : the binary form of a number . The two
's complement of a negative number : the binary form of the absolute value of a negative number, then negated and then added by 1.
Insert image description here

3.1.2、short int (short)

int bit 4 bytes, short int bit 2 bytes

3.1.3、long int (long)

int bit 4 bytes, long int bit 4 bytes

3.1.4、long long int (long long)

int bit 4 bytes, long int bit 8 bytes
Insert image description here

3.2 Character types

Characters are stored in their code (integer) form.
A=65
a = 97
Insert image description here

3.3 Floating point types

In C language, real numbers are stored in exponential form.
123e3 means 123 10^3
123e-3 means 123
10^-3
defaults to double type, and the one ending with f is float type (12.3f)
Insert image description here

3.4. Type conversion

3.4.1 Addition, subtraction, multiplication and division

1. Two numbers in addition, subtraction, multiplication and division have a float or double type, and the results are double.
2. To operate on int and float or double types, first convert the int type and float to double, and then perform the operation.
3. The operation of character (char) type data and integer type data means the operation of ASCII code and integer type data.
4. When there are signed numbers and unsigned numbers in the expression, they will become unsigned numbers and participate in the operation, and the result will also be an unsigned number.

3.4.2 Forced type conversion

(double) a Convert a to double type
(int)(x+y) Convert the value of x+y to int type

3.4.3 Automatic conversion

Types that occupy a small number of memory words are automatically converted to types that occupy a large number of memory bytes to ensure that the accuracy is not reduced.

3.4.5 Assignment statement

In an assignment statement, the right side of the equal sign is automatically converted to the type on the left side of the equal sign.

4. Format output characters

%d decimal signed integer
%x integer represented in hexadecimal
%f float type floating point number
%e exponential floating point number
%s string
%p pointer zhi
%u decimal unsigned integer
%o integer represented in octal
%lf double Type float
%c single character

Special usage:
%3d is less than 3 digits, fill in the front with spaces
%03d is less than 3 digits, fill in the front with 0
%-3d is less than three digits, fill in the back with spaces
%5.2f 5 digits in total, only 2 digits are retained after the decimal point , if there are insufficient integer digits, fill them with spaces, and use 0 for decimal digits.

5. Store related keywords

register、static、const、auto、extern

5.1 register

The meaning of register is that variables modified with register are register variables.
That is: when compiling, tell the compiler that this variable is a register variable, and try to allocate its storage space in a register.

1. The defined variables may not actually be stored in registers.
2. When the CPU fetches data, it is faster to retrieve the data from the register than from the memory.
3. Because registers are relatively precious, register arrays cannot be defined.
4. Register can only modify character types and integer types, but cannot modify floating point types.
5. Because the variable modified by register may be stored in a register, the address of the register variable cannot be obtained.
register int a;
int *p;
p = &a;//Wrong. a may not have an address.

5.2 static

static can modify global variables, local variables, and functions

5.3 const

const means constant. A variable modified with const is read-only and its value cannot be modified.
const can modify pointers.

5.4 auto

auto int a and int a are equivalent and are basically not used now.

5.5 extern

It means external. Generally used for declaration of functions or global variables.

6. Other keywords

sizeof typedef volatile

6.1 sizeof

Used to measure the size of the storage space occupied by variables and arrays.

6.2 typedef

Renaming is to rename an existing type.
typedef int INT16;
INT16 a; is equivalent to int a;

6.3 volatile

When the CPU uses a volatile variable, it retrieves it from the memory again to ensure that the latest value is used.

Naming rules: Identifiers in C language consist of alphanumeric characters and underscores. And it can only start with the letters or frighten Xi'an.

Guess you like

Origin blog.csdn.net/LookOutThe/article/details/133378826