The method of self-study in C language

Source: Internet
Author: User
Tags numeric value

a basic learning method for the introduction of C language
"C language" is rich in content, some of the details involved, such as hardware knowledge and data structure knowledge, self-study can not be exhaustive, otherwise will inevitably forgotten how, but can not grasp the principal contradiction. I think the beginner of C language candidates, beginning not to be too dead in every detail, and should be the main focus on the most basic, most commonly used those parts, after a certain basis and then deep into some non-major details, some details need to be mastered through longer-term practice. Beginner C language, you may encounter some problems to understand, please do not be discouraged, summon up the courage to the back of the content of learning, to learn the chapters behind the knowledge, the front of the problem will be solved. Learn C language always have to remember "dawn in front" and "daughter difficult to buy back to see", "daughter difficult to buy back to see" is the important method of learning knowledge, that is, learning the knowledge behind, do not forget to look back to understand the problems left behind and deepen understanding of the previous knowledge, which is the most difficult to learn, but it is the For example: In the C language is the most typical of the structure of the program design ideas, whether it is the kind of textbooks, the first emphasis on this method, you may not fully understand, but learn the function, then back to carefully understand, restudying, understand it is not so difficult. Learning C language is to go through a few iterations, before and after the run-through, accumulation should be mastered C knowledge.

In the following, we will focus on the characteristics of "C language", based on the 98 National Test syllabus, the key points and difficulties, from the macroscopic and microscopic two angles, on how to learn this course to provide some suggestions and methods for everyone to learn the reference.

First, we talk about the method of self-study C language from macroscopic.
I think to learn C language must first understand the level test C language Outline and content, as well as the use of various auxiliary materials, which is the basis for learning C. From previous experience and some information on the Internet, we must prepare the following information for self-study C language:

1, teaching materials: Everyone is now recognized as Tsinghua University rectification editor of the book, of course, there are other can also, such as the University Press.
2, Problem sets: I think Tsinghua University's "C-language sample of the Compendium" good. The answer is appended to the book.
3, on the computer practice: I think the Nankai University can, it is best to do it all over before the test. The answer can be downloaded to the test network download area.
4, Outline: This is sure to, you can go to the test network two-level version there, and then print it out.
5, self-study plan: In order to achieve a plan to learn C language, we can according to their own study (or work) situation, to develop a self-study plan, step by step study.
6, Simulation disk: In order to better familiar with the examination room environment, the next simulation disk is essential, can be in http://studywang.yeah.net/. Download, note, in the download of the key tray to come back together, or can not enter the test environment.
7, Teaching CD: If you can buy a C language teaching CD, it is certainly better, so that you can more intuitively learn C language.
From the micro point of view on the C language of a few key points and difficulties, one by one to talk about specific self-study methods.




second, how to learn the C language operators and the order of operations
C language is very rich in operation, the type of operation is much more than other programming language. Therefore, when a number of different operations constitute an expression of an operation, that is, when multiple operators appear in an expression, the precedence and binding rules of the operations are very important.

beginners often feel very difficult, think C language learning is too complex, in fact, as long as we classify this reasonably, to find out they and we learned in mathematics in the difference between the operations, remember that these operations are not difficult, some operators in understanding will remember the heart, the future use handy, And some can temporarily give up do not remember, and so on when used to remember not later.

The following is a list of all operators sorted by priority: (sorry, because the table is not normal, has been deleted by me everyone can read the book)

The table lists 15 precedence operators, from high to low, with priority 1 to 15, and the last side as a binding rule, except that the 2nd, 3, and 14th levels are combined from right to left, others are from left to right, which determines the order of operations of the sibling operator.
let's take a few examples to illustrate:
(1) 3*20/4%10 There are 3 operators in this expression, the sibling operators, which are combined from left to right, so that 3 * 20=60 is calculated first, then 4 is removed, the result is 15, and finally the% (remainder) operation, so the end result of the expression is 15%10 = 5
(2) a = 3;b = 5;c =++ A * b; d =a + +* B;
";" in the example. is the C language of the statement delimiter, the execution order is from left to right, the 1th statement executes after the value of a is 3, the 2nd statement executes after the value of B is 5, the 3rd statement has two operators in front + + and *, in the order listed in the table, + + First execute, * after execution, so + + a after execution, The value of a is 4, because the + + is the predecessor operation, so A's value 4 participates in the operation, the value of C is 20, and finally executes the 4th statement, because A + + is a post operation, so a value of 4 participates in the operation, so that the value of D is still 20, and a after the operation of the value of 1,
After this example is executed, a value of 5,b value of 5,c value of 20,d is also 20.
(3) a = 3,b = 5,b+ = A,c = b* 5
in the example, the "," is a comma-associative operation, called the comma expression, from left to right, the result of the last expression is the result of a comma expression, so the above comma expression result is 40,a value of 3,b value of 8,c value is 40.



three, how to learn the C language of the four kinds of program structure
(1) Sequential structure
The procedure of this kind of structure is simple, it is the mechanism that executes sequentially according to the order of the sentence. Sequential structures are executed from top to bottom and executed sequentially, so writing programs must follow this rule, or your program will not execute the results.

For example, a = 3,b = 5, now exchange a A, a, the correct program is:
C = A;
a = b;
B = C;
The result of the execution is a = 5,b = c = 3 If you change its order, write:
a = b;
C = A;
B = C;
then the execution result becomes a = b = c = 5, which cannot achieve the intended purpose, which is a common mistake for beginners.
sequential structure can be used to form a simple complete program, common input, calculation, output three-step program is a sequential structure, such as the calculation of the area of the circle, its program order is the input circle radius R, calculate s = 3.14159*r*r, the output circle area s. In most cases, the sequential structure is a part of the program, together with other structures to form a complex program, such as the block in the branch structure, loop structure in the loop body and so on.

(2) Branching structure
The branching structure differs from the sequential structure in that it chooses the execution path according to certain conditions rather than the physical order in which the statement appears. The key of the program design method of branch structure is to construct the proper branch condition and the analysis program flow, and select the appropriate branch statement according to the different program flow.
branch structure is suitable for the calculation with logical condition judgment, design such programs often must first draw its program flowchart, and then write the source program according to the program flow, so that the programming analysis and language separation, making the problem simple, easy to understand. The program flowchart is a flowchart based on the program drawn by the problem solving analysis.

The learning branch structure should not be confused by the branch nesting, as long as the basic branch structure is clarified, the nesting structure is not difficult. Nesting is nothing but branching and branching, not new knowledge, as long as you have a solid basic knowledge, branch nesting is difficult to you, below we focus on a few basic branch structure of learning methods.

①if (condition)
{
Block
}
A block in this branching structure can be a statement, at which time "{
} "can be omitted, or it can be multiple statements. It has two branch path optional, one is the condition is true, the execution block, the other is the condition is not satisfied, skip block.
For example, to calculate the absolute value of x, as defined by the absolute value, we know that when x>=0, its absolute value is constant, and x<0 when its absolute value is the inverse of x, so the program segment is: if (x<0)
x=-x;
②if (condition)
{Block 1}
Else
{Block 2}
This is a typical branching structure, if the condition is true, Block 1 is executed, otherwise the execution Block 2, Block 1 and block 2 have 1 or several statements.
such as: Seeking the root of ax^2+bx+c=0
analysis: Because when b^2-4ac>=0, the equation has two real roots, otherwise (b^2-4ac<0) has two conjugate complex root. Its program section is as follows:
d=b*b-4*a*c;
if (d>=0)
{x1= (-b+sqrt (d))/2a;
x1= (-b-sqrt (d))/2a;
printf ("x1=%8.4f,x2=%8.4f\n", x1,x2);
}
Else
{r=-b/(2*a);
I =sqrt (-D)/(2*a);
printf ("x1=%8.4f+%8.4fi\n" R, i);
printf ("x2=%8.4f-%8.4fi\n" R,i)
}
③ Branch: Its statement format is:
if (condition 1) {Block 1};
else if (condition 2) {Block 2}
else if (condition 3) {block 3}
...
else if (condition N) {block n}
else {block n+1}
④switch statement:
Switch
The statement is also a multi-branch SELECT statement, also known as the multi-switch statement, exactly which piece to execute, depending on the switch settings, that is, the value of the expression matches the constant expression of the way, it is different if-else statement, it all branches are tied, when the program executes, by the first branch to find, if it matches , executes the subsequent block, then executes the 2nd branch, the 3rd branch ... Block until the break statement is encountered, and if it does not match, find out whether the next branch matches.

(3) Cycle structure:
The cyclic structure can reduce the amount of repetitive writing of the source program, which is used to describe the problem of repeated execution of a certain algorithm, which is the most useful program structure in programming, and the C language provides four kinds of loops, namely Goto loop, while loop, Do–while Loop and for loop.
four loops can be used to deal with the same problem, generally they can replace each other, but generally do not mention Chang with the goto cycle, so we will focus on the other three kinds of loops.

The three commonly used circular structure learning focuses on figuring out their similarities and differences so that they can be used in different situations, so let's take a good look at the format and order of the three loops in the book, and how to replace them with examples of while loops, rewrite a program with a for statement, This will better understand their role.

Note: In the while and Do-while loop body and for
The 3rd statement in the loop should contain statements that tend to end (such as i++,i--), or it might be a dead loop, which is a common mistake for beginners.
let's discuss the similarities and differences between the three loops:
when looping with while and do-while, the initialization of the loop variable should precede the loop body, while the for loop is in statement 1;
loops and for loops are first judged by the expression, then the loop body is executed, and the Do-while loop is the expression after the loop body is executed, that is, the Do-while loop body is executed at least once, while the while
loops and for are not necessarily. These three loops can jump out of the loop with a break statement, end the loop with the continue statement, and the goto statement and if loop, cannot use break and
The continue statement is controlled.
these three kinds of structures are not isolated from each other, in the loop may appear branching, sequential structure, branching may also appear in the loop, the order structure and the loop, branching as a statement, it is a constituent of the order structure, so the three kinds of structure of each other, you can achieve a variety of algorithms, design a problem solving program, However, if a large number of topics, so that the program is often long, repetitive structure, and poor readability, so we often program C as a modular structure.

(4) Modular program Structure
C Language Modular program structure is implemented with functions, the complex C program is divided into several modules, each module is written into a legitimate C function, and then the main function call function and function call function to achieve a large C program: c program = Main (major) + several functions.

in the program design, some commonly used function modules are often written as functions, or large program segments can be divided into several functions, the former is to reduce the repetitive programming section of the workload, the latter is to shorten the length of the module, so that the program is easy to read.

A source program file consists of one or more functions, which is a compilation unit, and a C program consists of one or more source program files. For larger programs, often divided into multiple files, so that can be written separately, compiled separately, improve the efficiency of high debugging, a source program files can be more than a common C program.

C program is executed from the main () function, call other functions after the process back to the main function, in main to end the entire function run, the main function is called the system, the user can modify the contents of the function body, but cannot modify its name and parameters, A C program must have a main function and only one main function.

all functions are parallel, that is, a function that is independent of each other when defining a function does not belong to another function, that is, functions cannot be nested, but can be called each other, but cannot call the main function.

functions are divided into two categories, that is, standard functions and user-defined functions, standard functions, also known as library functions, provided by the system, the user can be directly called, C language provides a wealth of library functions, candidates in the C program to consult the textbook after the function description, which can save your programming effort; user-defined functions are written by the programmer.




Iv. How to learn the data type of C language
C language data type is very complex, difficult to learn, but also the focus of C language. The data types of the C language include basic types, constructed types, pointer types, and empty types.
(1) Basic type
① integral type: Basic integer, short integer, Long integer, and unsigned integral type.
These people should pay attention to the range of values, beginners often do not pay attention to the data out of bounds, so that the program is not working properly, or the value of the adjustment is small, it is not easy to see where their fault. For example, to convert a string to a numeric value, which generally exceeds two bytes, and the output is%d, so the output is inaccurate.
Another note is that the value can be treated as a character, and of course, the character can be processed in integers, with a value between -128--127 (or unsigned 0-256), which is a byte.
Another note is: We have to pay attention to the length of each data type in memory, if there is char a[10],*p;p=a;p++, such as each plus 1, in the memory of the address ordinal also add 1, but if it is an int, that every plus 1, in the memory of the sequence number is plus 2, the same long type is added 4.
②: Divided into single-precision and double-precision, in PC series microcomputer, single-precision accounted for 4 bytes, the effect is 7-bit, the data is 10^-38---10^38; double precision is 15-16 bits, the data range is 10^-308-10^308. If there is factorial, or power function, logarithmic function, radicals is generally used double precision type.
③ character: All characters in the C language can be in integers, but also specify that the signifier begins with ' \ ' three-bit 8-digit, or two-bit hexadecimal or one-letter character, such as ' \056 ', ' \19 ', ' \ t ', ' \ n ' is a character, it is a special character, The combined priority is left-to-right. Note: "\" is the end-of-string token.
(2) pointer type: pointer is the C language has a representative feature of one of the functions, C-language learning, mainly depends on your understanding of the pointer and use. A pointer is a variable that holds the address of another variable, so it can be seen that the pointer is a variable that occupies a certain amount of storage space just like a normal variable, but instead of storing the normal data in the pointer's storage space, it is an address. So we can use the pointer directly to the memory of a variety of data structures in the fast processing, and it provides a simple and convenient method for all kinds of data transmission between functions, the pointer is closely related to the computer system is a form of processing, therefore, the correct use of the use of the right hand can be used to develop a simple suction fast, strong performance, High-quality procedures. However, improper use of pointers will also lead to serious errors in program runaway, especially in the computer running this morbid program, often the destruction of the system, resulting in the system running out of control of the serious situation. Therefore, to fully understand and master the concept and use of pointers is one of the key elements in the study of C language programming.
Now let's look at an example that can help us understand pointers.
# include<stdio.h>
Main ()
{static char str[]= "program";
Char *ps;
While (*ps!= ')
{Putchar (*ps);
ps++;
}
Putchar (' \ n ');
}
The result of the operation is: program.
in this example, the pointer PS refers to the first address of the extraction character array str, ps++ represents the address of STR plus 1, so the output at the end of the loop is: program.
(3) Construction type:
① Array: The set of variables of a sequence relationship, with the same type of variables.
② Structure: A data composition of different data, which consists of several members, similar to the data structure in a database system, and each member is equivalent to a field.
③ Union: Data of different data types together use the same storage area. In a union's memory space, only one member item's data can be persisted at a time, and the Union occupies the maximum number of bytes of the member, while the structure occupies a memory space that is the sum of the bytes consumed by the stored member.
④ enumeration: Used to describe the range of values of a variable, that is, the description type of the collection.
(4) NULL type: Void is used to define a function with no return value.
Example: void Printstar ()
{...}
indicates that the function Printstar does not return any values when called. -------------------------------------turn from fish C forum

The method of self-study in C language

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.