Some misuse and summary of the C language of single-chip microcomputer

Source: Internet
Author: User

In the learning of a single-chip computer to really know what C is what it is to do ~ but the C language used to embedded only a small part of his application there are many places, hehe, we do not discuss this here. We are not in the process of writing a lot of mistakes even if the compiler passed the results of our expectations, it is not difficult to find out where it is wrong? My absolute language can be called language, it must be a tool to communicate with each other communication between the intention of the tool, As a language that must have its own grammar, to communicate with each other must first learn its grammar (such as expressions, functions, loops, pointers) I call the C language syntax. C language Although very strong but he also has a lot of traps, so I send this blog has two purposes one is: the C language some misuse of the wrong place to summarize, and the other is C language some basic grammar summed up ~First time:1About self-increment (i.e. ++i,i++To give a number plus one or minus one we can: I+=1; J-=1, while the C language also allows+ + and---operators are actually misleading here, because + + and--can be used as prefixes and suffixes, so that they can change the value of the operand, let's take a look at: I=1;p rintf ("i is%d\n", ++i);/*prints I is 2*/printf ("i is%d\n", i);/*prints I is 2*/Evaluate expression I++The result is I, but it causes I to then perform self-increment: I=1;p rintf ("i is%d\n", i++);/*prints I is 1/*/printf ("i is%d\n", i);/*prints I is 2*/The first printf shows the original value before I increment, and the second printf shows the new value after I has changed;--like I don't have an example.but used multiple times in the same expression+ + and--often difficult to understand we look at the following example: I=1; J=2; k= ++i + j + +The final value of I,j,k is 2, respectively.3, 4 and ++i is 2 J + +is 2; Summary: Whether it is++i or i++ Execute this statement after the value of I is added to the value of just (++i) plus one and (i++) has not changed,2. typedef and#define2.1the. TYPEDEFC language, in addition to using standard type names directly (such asint Char float Double) and the struct, the common body, the pointer, the enumeration type that you declare, you can also declare a new type name with a typedef in place of the existing type name. typedef unsignedCharu8;typedef unsignedintu16;u8 count;u16 time;typedefstruct{U8 month;u8 day;u16 year;} Date;date Brithday; Summarize the method that declares the new type name:1The definition body is written by defining the variable (e.g. unsignedinti)2Replace the variable name with a new variable name (e.g. replace I with U16)3At the front, add a typedef (typedef unsignedintU16)4. Then define the variable with the new type name2.2 #define2.1. 1 macro definition without parameters#defineIdentifier string#definePI 3.1415926Note:1Its role is to replace 3 with the specified identifier pi in this program.14159262The macro definition is to use a macro instead of a string to do a simple permutation, not to do a correctness check if written#definePI 3.l4l6926that is, 1 is written in the letter L but the preprocessing will not do any grammar check as usual!!2.1. 2 macro definition with parameters#defineMacro name (parameter) string#defineS (A, b) a*b Area=S (A, b);#defineMAX (x, y) > (y)? (x) y)3the difference between typedef and # define is generally typedef because it handles pointer type typedef correctlyChar*String1;#defineString2 char *String1 s1,s2; String2 S3,S4;S1,S2,S3 is defined for Char*but S4 is defined for char type3.Staticvariable static variables are broadly divided into three usages1for local variables, it becomes a static local variable. Static local variables have two usages, memory functions, and global lifetimes.2for global variables, the primary role is to restrict this global variable from being called by other files.3for members in a class. Indicates that this member belongs to this class but does not belong to any particular object in the class1Static local variables are static local variables that are stored statically and have the following characteristics: (1A static local variable defines its lifetime as the entire source program within a function, but its scope is still the same as the automatic variable, which can only be used within the function that defines the variable. After exiting the function, it cannot be used even though the variable continues to exist. (2allows the static local amount of the constructed class to be assigned an initial value, such as an array, which is automatically assigned 0 by the system if no initial values are assigned. (3The system automatically assigns a 0 value to a static local variable of the underlying type if it is not assigned the initial values in the description. The value of the automatic variable is indeterminate if it is not assigned the initial values. According to the characteristics of static local variables, it can be seen that it is a lifetime of the whole source of the program amount. Although it cannot be used after the function that defines it, it can continue to be used if the function that defines it is called again, and the value left after the previous call is saved. Therefore, you can consider static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although this can be achieved with global variables, global variables sometimes cause unintended side effects, so it is advisable to use local static variables. Examples are as follows:voidFun () {Static intA =1; a++;} The first time you enter this function, variable A is initialized to 1.!and then self-increment 1, each time you enter the function, a will not be initialized again, only 1 of the operation; before the static invention, to achieve the same function, you can only use global variables:intA =1;voidFun () {a++;}2Static global variables (external variables) are then added to static global variables by adding static. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files. From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use. So the function of the static descriptor is different in different places. 3. Static class member variable the static keyword has two meanings, and you look at the context to Judge1The. Indicates that the variable is a static storage variable, indicating that the variable is stored in a static storage area.2indicates that the variable is an internal connection (in this case the variable is not within any {}, as in global variables, this time with static), that is, in other. cpp files, the variable is not visible (you cannot use it). Ii.Staticfunctions--intrinsic and external functions when a source program consists of multiple source files, the C language is divided into internal functions and external functions according to whether the function can be called by functions in other source files. 1intrinsic function (also called static function) if a function defined in a source file can only be called by a function in this file, and not by a function in the other file of the same program, this function is called an intrinsic function. To define an intrinsic function, simply add one before the function type.StaticKeyword , as shown below:Staticfunction type function name (function parameter table) {...} Keyword "Static", the translation into Chinese is" static ", so the internal function is also called the static function. But here "Static"does not mean the means of storage, but refers to the scope of the function is limited to this file. The advantage of using intrinsic functions is that when different people write different functions, they don't have to worry about the functions they define, or whether they have the same name as the functions in other files, because the same name doesn't matter. 2definition of external function outside functions: when defining a function, if no keyword is added "Static", or labeled keyword"extern", which indicates that this function is an external function: [extern] Function type function name (function parameter table) {...} When calling an external function, you need to describe it: [extern] Function type function name (parameter type table) [, Function name 2 (parameter Type table 2) ...];

Some misuse and summary of the C language of single-chip microcomputer

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.