A complete roadmap for learning C ++

Source: Internet
Author: User
Tags ibase

/* It is recommended to a friend who wants to learn C ++ a simple but complete roadmap for learning C ++:

C ++ primer-> C ++ standard library-> Objective C ++-> Objective STL-> exploring C ++ object models

C program common algorithm source code

Algorithm (algorithm): basic ideas, methods, and steps for solving computer problems. Algorithm Description: describes the methods and steps taken to solve a problem or to complete a task, including the required data (input data and output results) what structure is used, what statements are used, and how to arrange these statements.

Generally, the algorithm is described using natural language, structured flowchart, and pseudocode.

I. Simple algorithms for counting, summation, and factorial

Cycle is used for such problems. You must determine the initial value, end value, or end condition of the cyclic variable based on the problem. You must also pay attention to the initial value of the variable used to represent the count, sum, and factorial.

For example, a random function is used to generate 100 random integers in the range of [0, 99]. The numbers in a single digit are 1, 2, 3, 4, 5, 6, 7, 8, respectively, and print out the numbers of 9 and 0.

This topic uses Arrays for processing. array a [100] is used to store 100 random integers. array X [10] is used to store numbers of 1, 2, 3, 4, 5, 6, respectively, the number of numbers 7, 8, 9, and 0.

That is, the number of a single digit is 1 in X [1], and the number of a single digit is 2 in X [2 ,...... The number of digits with a single digit being 0 is stored in X [10].
Void main ()
{
Int A [101], X [11], I, P;

For (I = 0; I <= 11; I ++)
X= 0;

For (I = 1; I <= 100; I ++)
{
A = rand () % 100;
Printf ("% 4d", );
If (I % 10 = 0) printf ("\ n ");
}

For (I = 1; I <= 100; I ++)
{
P = a % 10;
If (p = 0) p = 10;
X [p] = x [p] + 1;
}

For (I = 1; I <= 10; I ++)
{
P = I;
If (I = 10) p = 0;
Printf ("% d, % d \ n", p, x );
}
Printf ("\ n ");
}

2. Calculate the maximum common divisor and least common multiple of two integers.

Analysis: the algorithm for finding the maximum common approx: (least common multiples = product of two integers/maximum common approx)

(1) For known two numbers m, n, making m> n;

(2) divide m by n to obtain the remainder r;

(3) If r = 0, n is the obtained maximum common approx. The algorithm ends; otherwise, the algorithm is executed (4 );

(4) m records n, n records r, and then repeated execution (2 ).

For example, calculate the maximum common approx. m = 14, n = 6. M n r

14 6 2

6 2 0
Void main ()
{
Int nm, r, n, m, t;
Printf ("please input two numbers: \ n ");
Scanf ("% d, % d", & m, & n );
Nm = n * m;
If (m <n)
{
T = n;
N = m;
M = t;
}

R = m % n;
While (r! = 0)
{
M = n;
N = r;
R = m % n;
}
Printf ("maximum common Appointment: % d \ n", n );
Printf ("minimum public multiple: % d \ n", nm/n );
}

3. Determine prime numbers

The number that can only be divided by 1 or itself is called a prime number. The basic idea is to use m as a division,

Use 2 to sqrt (m) as the divisor. If they are not all divided, m is the prime number. Otherwise, m is not. (Available in the following sections)
Void main ()
{
Int m, I, k;
Printf ("please input a number: \ n ");
Scanf ("% d", & m );
K = sqrt (m );
For (I = 2; I <k; I ++)
If (m % I = 0) break;
If (I> = k)
Printf ("this number is a prime number ");
Else
Printf ("this number is not a prime number ");
}

Write it into a function. If it is a prime number, 1 is returned. If it is not, 0 is returned.
Int prime (int m)
{
Int I, k;
K = sqrt (m );
For (I = 2; I <k; I ++)
If (m % I = 0) return 0;
Return 1;
}

4. Verify godebach's Conjecture

(Any even number greater than or equal to 6 can be divided into the sum of two prime numbers)

Basic Idea: If n is an even number greater than or equal to 6, it can be divided into two numbers: n1 and n2. Check whether n1 and n2 are prime numbers. If they are both, they are a group of solutions. If n1 is not a prime number, you do not have to check whether n2 is a prime number.

First from n1 = 3, check whether n1 and n2 (n2 = N-n1) are prime numbers. Then enable n1 + 2 to check whether n1 and n2 are prime numbers ,...

Until n1 = n/2.

Using the above prime function, the program code used to verify the godebach conjecture is as follows:
# Include "math. h"
Int prime (int m)
{
Int I, k;
K = sqrt (m );
For (I = 2; I <k; I ++)
If (m % I = 0) break;
If (I> = k)
Return 1;
Else
Return 0;
}

Main ()
{
Int x, I;
Printf ("please input a even number (> = 6): \ n ");
Scanf ("% d", & x );
If (x <6 | x % 2! = 0)
Printf ("data error! \ N ");
Else
For (I = 2; I <= x/2; I ++)
If (prime (I) & prime (x-I ))
{
Printf ("% d + % d \ n", I, x-I );
Printf ("Verification Successful! ");
Break;
}
}

V. sorting problems

1. sort by selection (ascending)

Basic Idea:

1) For a sequence with n numbers (stored in array a (n), the minimum number is selected and the position is exchanged with the 1st number;

2) except for 1st, the smallest number is selected among the other n-1 numbers, and the position is exchanged with 2nd;

3) and so on. After N-1 is selected, the sequence is sorted in ascending order.

The program code is as follows:
Void main ()
{
Int I, j, imin, s, a [10];
Printf ("\ n input 10 numbers: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & );

For (I = 0; I <9; I ++)
{
Imin = I;
For (j = I + 1; j <10; j ++)
If (a [imin]> a [j]) imin = j;

If (I! = Imin)
{
S =;
A = a [imin];
A [imin] = s ;}
Printf ("% d \ n", );
}
}
}

2. bubble sort (ascending)

Basic Idea: (compare two adjacent numbers and adjust them to the front if they are small)

1) There are n numbers (stored in array a (n). The first line is to compare each adjacent two numbers, and a small value is adjusted to the front. After the n-1 adjacent comparisons, the maximum number is already "bottom", placed in the last position, and the decimal point is "floating up ";

2) for the second round, compare the remaining n-1 numbers (the maximum number is already "bottom") by the above method,

The number of times larger after the comparison of The N-2 times;

3) by analogy, n numbers are compared in a total of N-1 bits, and n-j times are compared in the j bits.
The program section is as follows:
Void main ()
{
Int a [10];
Int I, j, t;
Printf ("input 10 numbers \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & );

Printf ("\ n ");
For (j = 0; j <= 8; j ++)
For (I = 0; I <9-j; I ++)
If (a> a [I + 1])
{
T =;
A = a [I + 1];
A [I + 1] = t;
}

Printf ("the sorted numbers: \ n ");

For (I = 0; I <10; I ++)
Printf ("% d \ n", );
}

3. sort by merging (merge two ordered arrays A and B into another ordered array C in ascending order)

Basic Idea:

1) Compare the first element in array A and array B, and add small elements to array C;

2) Compare the next element in the array where the small element is located with the element larger after the last comparison in the other array,

Repeat the above comparison process until an array is ranked first;

3) copy the remaining elements of another array into the C array and complete the merging and sorting.

The program section is as follows:
Void main ()
{
Int A [10], B [10], C [20], I, IA, IB, IC;
Printf ("Please input the first array: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & );

For (I = 0; I <10; I ++)
Scanf ("% d", & B );

Printf ("\ n ");
IA = 0; IB = 0; Ic = 0;
While (IA <10 & IB <10)
{
If (A [Ia] <B [IB])
{
C [IC] = A [Ia];
IA ++;
}
Else
{
C [IC] = B [IB];
IB ++;
}
IC ++;
}

While (IA <= 9)
{
C [IC] = A [Ia];
IA ++; IC ++;
}

While (ib <= 9)
{
C [IC] = B [IB];
B ++; IC ++;
}

For (I = 0; I <20; I ++)
Printf ("% d \ n", C );
}

6. Search for Problems

1. ① sequential Lookup (search for a certain number of X columns)

Basic Idea: place the number of columns in array a [1] -- A [n], put the number to be searched in array X, and compare the elements of array X and array a from start to end. Use the Variable P to represent the element subscript of array A. The initial value of P is 1, so that X is compared with a [p]. If X is not equal to a [p], P = p + 1, repeat this process continuously. Once X is equal to a [p], the loop is exited. In addition, if p is greater than the array length, the loop should also be stopped. (This process can be implemented by the following statement)
Void main ()
{
Int A [10], p, X, I;
Printf ("Please input the array: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & );

Printf ("Please input the number you want find: \ n ");
Scanf ("% d", & X );
Printf ("\ n ");
P = 0;
While (x! = A [p] & p <10)
P ++;
If (p> = 10)
Printf ("the number is not found! \ N ");
Else
Printf ("the number is found the no % d! \ N ", p );
}

Thought: rewrite the search function "Find" in the above program. If it is found, the lower mark value is returned. If it is not found,-1 is returned.

② Basic idea: place a column in array a [1] -- a [n]. The key value to be searched is the key, and compare the key with the elements in array a from start to end, if they are the same, the search is successful. If they cannot be found, the search fails. (The procedure is as follows. Index: stores the subscript of the element to be found .)
Void main ()
{
Int a [10], index, x, I;
Printf ("please input the array: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & );

Printf ("please input the number you want find: \ n ");
Scanf ("% d", & x );
Printf ("\ n ");
Index =-1;
For (I = 0; I <10; I ++)
If (x =)
{
Index = I;
Break;
}

If (index =-1)
Printf ("the number is not found! \ N ");
Else
Printf ("the number is found the no % d! \ N ", index );
}

2. Semi-query (only sequential series can be searched)

Basic Idea: set n orders (from small to large) to be stored in array a [1] -- a [n]. The number to be searched is x. use the variables bot, top, and mid to indicate the bottom of the data range (lower limit of the array)

Top (upper bound of the array) and middle, mid = (top + bot)/2, the algorithm for half-lookup is as follows:

(1) x = a (mid), the exit loop is found; otherwise, the following judgment is made;

(2) x <a (mid), x must fall within the range of bot and mid-1, that is, top = mid-1; (3) x> a (mid ), x must fall within the range of mid + 1 and top, that is, bot = mid + 1;

(4) After a new search range is determined, repeat the above comparison until it is found or bot <= top.

Write the above algorithm into the following program:
Void main ()
{
Int a [10], mid, bot, top, x, I, find;
Printf ("please input the array: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & );

Printf ("please input the number you want find: \ n ");

Scanf ("% d", & x );
Printf ("\ n ");
Bot = 0; top = 9; find = 0;

While (bot <top & find = 0)
{
Mid = (top + bot)/2;
If (x = a [mid])
{
Find = 1;
Break;
}
Else
If (x <a [mid])
Top = mid-1;
Else
Bot = mid + 1;
}

If (find = 1)
Printf ("the number is found the no % d! \ N ", mid );
Else
Printf ("the number is not found! \ N ");
}

VII. Insertion Method

Insert a number into an ordered sequence. After insertion, the sequence is still ordered.

Basic Idea: n ordered numbers (from small to large) are stored in array a (1)-a (n), and the number to be inserted is x. first, determine the position P where x is inserted in the array. (It can be implemented by the following statement)
# Define N 10
Void insert (int a [], int x)
{Int p, I;
P = 0;
While (x> a [p] & p <N)
P ++;
For (I = N; I> p; I --)
A [I] = a [I-1];
A [p] = x;
}
Main ()
{Int a [N + 1] = {1, 3, 4, 7, 8, 11, 13, 18, 56, 78}, x, I;
For (I = 0; I <N; I ++) printf ("% d,", a [I]);
Printf ("\ nInput x :");
Scanf ("% d", & x );
Insert (a, x );
For (I = 0; I <= N; I ++) printf ("% d,", a [I]);
Printf ("\ n ");
}

8. Matrix (two-dimensional array) Operations

(1) addition and subtraction of Matrices

C (I, j) = a (I, j) + B (I, j) Addition

C (I, j) = a (I, j)-B (I, j) Subtraction

(2) matrix multiplication

(Matrix A has M * L elements, and matrix B has L * N elements, then matrix C = A * B has M * N elements ). Any element in matrix C (I = 1, 2 ,..., M; j = 1, 2 ,..., N)
# Define M 2
# Define L 4
# Define N 3
Void mV (int A [m] [L], int B [l] [N], int C [m] [N])
{Int I, J, K;
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
{C [I] [J] = 0;
For (k = 0; k <L; k ++)
C [I] [J] + = A [I] [k] * B [k] [J];
}
}
Main ()
{Int A [m] [l] = {1, 2, 3, 4}, {1, 1 }};
Int B [l] [N] = {, 1}, {, 1}, {, 1}, {, 1 }}, c [m] [N];
Int I, J;
MV (A, B, C );
For (I = 0; I <m; I ++)
{For (j = 0; j <n; j ++)
Printf ("% 4D", C [I] [J]);
Printf ("\ n ");
}
}

(3) matrix transfer

For example, a two-dimensional array A (5, 5) can be transposed in the following two ways:
# Define N 3
Void merge (INT a [n] [N])
{Int I, j, T;
For (I = 0; I <N; I ++)
For (j = I + 1; j <N; j ++)
{T = a [I] [j];
A [I] [j] = a [j] [I];
A [j] [I] = t;
}
}
Void ch2 (int a [N] [N])
{Int I, j, t;
For (I = 1; I <N; I ++)
For (j = 0; j <I; j ++)
{T = a [I] [j];
A [I] [j] = a [j] [I];
A [j] [I] = t;
}
}
Main ()
{Int a [N] [N] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, I, j;
Round (a);/* or ch2 ();*/
For (I = 0; I <N; I ++)
{For (j = 0; j <N; j ++)
Printf ("% 4d", a [I] [j]);
Printf ("\ n ");
}
}

(4) Find the smallest element in a two-dimensional array and the basic idea of its rows and columns in the same dimension array, which can be implemented in the following program section (take the two-dimensional array a [3] [4] as an example ): 'maximum, row, and column values are stored in variable max.
# Define N 4
# Define M 3
Void min (int A [m] [N])
{Int min, row, column, I, J;
Min = A [0] [0];
Row = 0;
Column = 0;
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
If (A [I] [J] <min)
{Min = A [I] [J];
Row = I;
Column = J;
}
Printf ("min = % d \ Nat row % d, column % d \ n", Min, row, column );
}
Main ()
{Int A [m] [N] = {, 45,-5 }};
Min ();
}

IX. Iterative Method

Algorithm idea: for solving a problem X, a new value X1 can be obtained from a given initial value x0 based on an iteration formula, the new value X1 is closer to the required value x than the initial value x0. Then, the new value X1 → x0 is used as the initial value, and the original method is used to calculate X1, repeat this sum until | x1-x0 | <ε (a given precision ). In this case, X1 can be used as the solution to the problem.

For example, the square root of a number is obtained by iteration. The formula for finding the square root is:
# Include <math. h>
Float fsqrt (float)
{Float x0, x1;
X1 = A/2;
Do {
X0 = x1;
X1 = 0.5*(x0 + A/X0 );
} While (FABS (x1-x0)> 0.00001 );
Return (X1 );
}
Main ()
{Float;
Scanf ("% F", & );
Printf ("genhao = % F \ n", fsqrt ());
}

10. Digital Conversion

Converts a decimal integer m to A → R (2-16) hexadecimal string.

Method: Remove m from R continuously until the quotient is zero and obtain the result in reverse order. The following is a conversion function. The IDEC parameter is a decimal number, and the iBase parameter is the base to be converted to a number (for example, the binary base is 2 and the octal base is 8 ), the function output result is a string.
Char * trdec (int idec, int IBASE)
{Char strdr [20], T;
Int I, IDR, P = 0;
While (IDEC! = 0)
{IDR = IDEC % IBASE;
If (IDR> = 10)
Strdr [p ++] = idr-10 + 65;
Else
Strdr [p ++] = IDR + 48;
IDEC/= IBASE;
}
For (I = 0; I <p/2; I ++)
{T = strdr [I];
Strdr [I] = strdr [p-i-1];
Strdr [p-i-1] = t;
}
Strdr [p] = '\ 0 ';
Return (strdr );
}
Main ()
{Int x, d;
Scanf ("% d", & x, & d );
Printf ("% s \ n", trdec (x, d ));
}

11. General string processing

1. Simple encryption and decryption

The idea of encryption is to add (or subtract) an ordinal K for each letter C, that is, replace it with the K letter after it. The conversion formula is as follows: c = c + k. For example, if the ordinal k is 5, then A → F, a → f, B →? G... When the letter after the ordinal number exceeds Z or z, c = c + k-26 For example: you are good → Dtz fwj ltti decryption is the inverse process of encryption. Each letter C is reduced (or added) by an ordinal K, that is, c = c-k. For example, the ordinal k is 5, then Z → U, z → u, Y → T... When the letter after the ordinal number is smaller than A or a, c = c-k + 26 is encrypted:
# Include <stdio. h>
Char * jiami (char stri [])
{Int I = 0;
Char strp [50], ia;
While (stri [I]! = '\ 0 ')
{If (stri [I]> = 'A' & stri [I] <= 'Z ')
{IA = stri [I] + 5;
If (IA> 'Z') IA-= 26;
}
Else if (stri [I]> = 'A' & stri [I] <= 'Z ')
{IA = stri [I] + 5;
If (IA> 'Z') IA-= 26;
}
Else IA = stri [I];
STRP [I ++] = IA;
}
STRP [I] = '\ 0 ';
Return (STRP );
}
Main ()
{Char s [50];
Gets (s );
Printf ("% s \ n", jiami (s ));
}

2. count the number of text words

Enter a line of characters to count the number of words. Words are separated by cells.

Algorithm ideas:

(1) extract a character from the left of the text (string). Set the logic to "word" to indicate whether the obtained character is a character in the word. The initial value is set to 0.

(2) If the character is not a separator for words such as "space", "comma", "Semicolon", or "Exclamation point", then judge whether the word is 1, if the word value is not 1, the Table begins with a new word. Let the number of words num = num + 1, And let word = 1;

(3) If the characters used are delimiters of words such as "space", "comma", "Semicolon", or "Exclamation point", it indicates that the characters are not characters in the word, so that word = 0;

(4) take the next character in sequence, and then (2) (3) until the end of the text.

The following section describes the number of words contained in a string.
# Include "stdio. h"
Main ()
{Char c, string [80];
Int I, num = 0, word = 0;
Gets (string );
For (I = 0; (c = string [I])! = '\ 0'; I ++)
If (c = '') word = 0;
Else if (word = 0)
{Word = 1;
Num ++ ;}
Printf ("There are % d word in the line. \ n", num );
}

12. Exhaustion

The basic idea of the exhaustive method (also known as the enumeration method) is to list various possible situations one by one and determine which one is the solution that meets the requirements, this is a "method without other solutions" and the most "stupid" method. However, it often works for problems that cannot be solved by parsing, loop is usually used to solve the problem of exhaustion.

For example, if you want to change the equivalent value of RMB 100 to RMB 100 for each of the 5-yuan, 1-yuan, and 0.5-yuan notes, you must make no less than one note. Which combinations are there?
Main ()
{Int I, j, k;
Printf ("5 yuan, 1 yuan, 5 horns \ n ");
For (I = 1; I <= 20; I ++)
For (j = 1; j <= 100-i; j ++)
{K = 100-i-j;
If (5 * I + 1 * j + 0.5 * k = 100)
Printf ("% 3d % 3d % 3d \ n", I, j, k );
}
}

13. Recursive Algorithms

Describe itself with its own structure, called Recursion

VB allows you to call yourself within the definition of a Sub-process and Function process, that is, Recursive Sub-process and recursive Function. Recursive processing is generally implemented using stacks. Each call is performed on itself, the current parameter is pushed to the stack until the recursion ends. The current parameter is popped up from the stack until the stack is empty.

Recursive conditions: (1) recursive end conditions and values at the end of the condition; (2) values can be expressed in recursion form and the recursion Develops Toward the termination condition.

For example, compile fac (n) = n! Recursive functions

Int fac (int n)
{If (n = 1)
Return (1 );
Else
Return (n * fac (n-1 ));
}
Main ()
{Int n;
Scanf ("% d", & n );
Printf ("n! = % D \ n ", fac (n ));
}

After signing in to work, the employer promises to be embedded, which makes me happy, and the employer is very responsible for the consideration of our recent graduates, commissioned a well-known training institution in Northeast China to provide us with expensive training. C language is indispensable, of course, so I bought this book "Science and Art in C Language" to review C. although there are not many new ideas in the intermediate C language tutorials explained by a doctoral student, there is always a long and obsolete knowledge, which is posted to remind myself to "learn new things ".

1. since I learned C ++, I have never used the printf function of C. Compared with std: cout, printf cannot determine the type of output and perform memory operations, it is dangerous.

In the prototype of printf, the parameter is a constant string + ......; The return value is an integer.

Note the output formats of constant strings: Common % d, % f, and % s are not required, but % g and % need to be described as follows: % g indicates common type, the value is displayed in a shorter value in the formula % f and % egex. If the output value cannot be determined in advance, it is the best output format. For example, if a float variable is defined, if it is an integer, the output in % g format will not display a bunch of 0 values, as if it is output in % d format, if it is a small value, it does not add a bunch of zeros, which makes people look very friendly.

% Is the percentage output representation, instead of using escape characters \ as we normally do \.

Precision Control:

Negative sign: indicates that the value is left aligned without a negative sign;

Width: Minimum number of characters in the output field. If the value to be displayed occupies less space, it is filled with spaces. If the value is too large to be displayed in a specified field, the field width is extended until it can accommodate this value.

Decimal point precision: For % g, the precision parameter specifies the maximum valid bit; for % f and % e, the precision parameter specifies the number of digits after the decimal point; for % s, the number of digits displayed in the string.

Note: one decimal point!

The return value is int. In Windows, the number of characters output by the printf function or the related bytes with an output error are displayed. Note that, although no definite characters are displayed in the output, it is also a single character!

2. About scanf () Functions

This function cannot identify the input type. Therefore, the specified type, such as % d and % F, must be displayed in the parameter table. However, this function differs from printf in % F, in the output, % F can output the double and float types, but in the input, the number of digits of the double type is significantly greater than that of the float type, so if it is like this:

Double D;

Scanf ("% F", & D );

Printf ("% F", d );

% Will be output! $ % # ^ @ # % A bunch of Alibaba Cloud items, the reason is the type width problem, correct is % lf output.

In a 32-bit Windows system, after sizeof, short 2; int 4; float 4; Double 8; but what I don't understand is that long double is also 8 (why should it be bigger than double );

In a 32-bit Linux system, only long double after sizeof is 12 different from Win32, which makes me feel less pleased.

Note that the Width limit can be added for the input, but the precision limit cannot be added;

For example: Char C, D; scanf ("% 3C, % 3C", & C, & D); printf ("% C, % d", c, d ); if abcdef [enter] is input, it is determined that ABC is assigned to C as a, and Def is assigned to D as D.

3. getchar and putchar

Both functions process one character at a time and have previously written:

While (c = getchar ())

{Putchar (c );

Putchar ('\ n ');......}

This time I discussed with a classmate that, according to common sense, I should enter a character and output a character immediately. But when we input a string, this string is output one by one.

Since both functions can be implemented by the above two functions, I wonder if scanf is used in putchar, and then put the input string in a buffer like 2, and then intercept them in sequence, processing in turn.

If you want to input one character and output one character immediately, the system may need to detect the keydown interruption for processing.

I still don't understand this question. I hope anyone can give me some advice if they want to pass! Thank you.

A philosopher once said, "It is dangerous to know half a solution ." My recent experiences with this sentence have become more and more profound.

Mr. Lin Yutang said, "It is not clever to use only one thing and not understand its principles ." Be cautious!

Global variables & local variables

When a global variable and a local variable have the same name, the global variable is masked. Local variables with the same name will be used when this variable is referenced in the function, instead of using global variables (if you want to reference global variables, add ::). In addition, local variables can be masked to each other, such as if nesting and for nesting.

This is related to the scope of local variables, and the scope depends on the implementation of each compiler. This is related to the compilation principles (by the way, review)

Generally, the symbol table of the identifier must contain several attributes: name, type, storage category, scope, visibility, and storage allocation information.

1) Name:

Generally, duplicate names are not allowed in symbol tables. If a program has a duplicate identifier, the program processes the name based on the definition of the language according to the scope and Visibility rules of the identifier.

If the program allows overloading, the function names are distinguished by the number, type, and return value of their parameters.

2) Type:

Determines the storage format of variable data in the bucket, and also determines the operation that can be applied on the variable.

3) Storage category:

The definition method has two forms: specifying keywords, variable definition + location.

This attribute is an important basis for semantic processing, checking, and storage allocation in the compilation process. It also determines its role domain visibility lifecycle and other issues.

4) Scope and visibility:

Generally, the scope of a variable is where the variable can appear. The function parameters are processed as internal variables of the function. The program structure itself contains statements for local variable declaration.

5) storage allocation:

The Compilation Program generally determines the storage area to be allocated for each variable and the specific location of the region based on the Storage Class of the identifier and the location and order of their appearance.

Static storage zone public static zone public + External whole Domain Function

Local static area local static area

Dynamic Storage Zone

The location and sequence of the identifier in the source program determine the specific location of the identifier in the storage area, that is, an offset.

About creating a symbol table:

Such as program:
Main ()
{Int a = 1, float c = 0.1;
{
Float a = 1.0;
{Float x = 5.5, B = 7.1 ;}
{Int B = 9; c = a + B + c ;}
}
}

1. Create the main point from the first layer of the symbol table to the memory;

2. Create the second layer of the symbol table to point to a and c in the memory, pointing to;

3. Create a point for the layer-3 symbol table to a in the memory, pointing to a; (the layer-2 a is different from the layer-3)

4. create a pointer from the fourth layer of the symbol table to x and B in the memory. When a bracket is encountered, it indicates that the current layer ends, the end item in the memory is established, and then a pointer is created from the end item, point to the last element in the upper layer, that is, the of the third layer, and then delete the pointer from the symbol table of the current layer to the memory;

......

N. The final result is that there are three points in the symbol table to the memory, pointing to a and a of main, a and c respectively, and then many end items refer back to the first three layers.

(Because images are not used in the blog, they cannot be marked more vividly. .)

Note: In different C files, the global variables with the same name are declared in static mode. To run normally, only one C file must be assigned a value for this variable. In this case, the connection will not go wrong.

Supplement:

Differences between Static global variables and common global variables

Global variables themselves are static storage methods, and static global variables are of course the same. There is no difference in storage methods between the two.

The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each file, static global variables limit their scopes, that is, they are valid only in the source file defining the variable and are not available in other source files of the same source program.

Because the scope of static global variables is limited to one source file, they can only be used by functions in the file, and some errors caused by other files can also be avoided.

Differences between Static local variables and common local variables

Static local variables are stored in different ways than common local variables, so their lifecycles are also different. The latter is dynamic and stored in the stack.

Globally distributed in the static zone and dynamically distributed in the heap.

Differences between Static functions and common functions
The Static function has only one copy in the memory, and the normal function will have one copy each time it is called.

*//*

Summary of the essence of C language!

Ciday (I)

I have been studying Computer for almost two years since I was a graduate student. Along the way, I have had a lot of GAINS and a lot of regrets. Now I have just had some leisure time, I want to leave some footprints and memories on the road I 've traveled. Everyone has their own different lives. Speaking of this, it is a procedural life. Goethe said in "FUDE": "If you have never chewed bread in sorrow, never wait for tomorrow in the cry, such a person does not know you-the power of heaven." So I want to write down some program life that brings me sorrow and tears. In fact, it is very important to study basic computer courses, such as discrete mathematics, compilation principles, operating systems, and formal languages ......, If you have walked these steps carefully, you will find that your path will become wider and wider in the days to come. Your previous efforts and sweat will continuously inspire you and give you support, give you the weapon and courage to move forward. You will find that many achievements will be made in the future!

For programming languages, I like C ++. It can give you the ultimate intellectual pleasure that other languages cannot give you. Of course, it will also give you the devil-like torment that a language can give you. In fact, Java, C #, and Python are also very good, and I like them very much. They are all very successful languages, and I have never been willing to become a blind believer in a language. each language has its successes, where it fails, and where it fits, unsatisfactory. So every time I see an article about the evaluation language, I will read it, but I will never speak.

C ++'s past life was C, and the mysteries and conciseness of C ++ are blue and blue! C brings too much confusion and flexibility, even if a high-level C programmer with several years of experience is still likely to be overturned in the small water ditch of C language. However, the C language is really not difficult. Next I want to point out the four most mysterious and changing areas in the C language, and they will continue to be unpredictable in the C ++ language.

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.