Some misuse and knowledge summarization of C language

Source: Internet
Author: User
Tags float double

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:

1. About self-increment (i.e. ++i,i++)

To give a number plus one or minus one we can:

i + = 1;

J-= 1;

The C language is also allowed to use + + and--operators, in fact, this is misleading, 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;

printf ("i is%d\n",++i); /* Prints I is 2 */

printf ("i is%d\n",i); /* Prints I is 2 */

The result of evaluating the expression i++ is I, but I am then raised by the I following the self-increment:

i = 1;

printf ("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; of course--like I don't have an example.

But using + + in the same expression multiple times--often difficult to understand we look at the following example:

i = 1;

j = 2;

K = ++i + j + +;

I,j,k final value is 2,3,4 and ++i is 2 J + + is 2;

Summary: Either ++i or i++ executes this statement after the value of I is added to the value of just (++i) plus one and (i++) unchanged,

2.typedef and # define

2.1.typedef

In addition to using standard type names directly (such as int char float double) and the struct, common body, pointer, enum type that you declare, the C language can also use typedef to declare a new type name in place of the existing type name.

typedef unsigned char U8;

typedef unsigned int u16;

U8 Count;

U16 time;

typedef struct

{

U8 Month;

U8 Day;

U16 Year;

}date;

DATE Brithday;

Summarize the method that declares the new type name:

1. First write the definition of the variable by defining the method (e.g. unsigned int i)

2. Replace the variable name with a new variable name (e.g. change I to U16)

3. Add typedef at the front (typedef unsigned int u16)

4. Then define the variable with the new type name

2.2 #define

2.1.1 macro definition with no parameters

#define Identifier string

#define PI 3.1415926

Attention:

1. Its role is to replace 3.1415926 with the specified identifier pi in this procedure.

2. Macro definition is to use a macro instead of a string, that is, to do a simple permutation, do not check the correctness if written

#define PI 3.l4l6926

That is, 1 is written in the letter L but the preprocessing as usual will not do any grammar check!!

2.1.2 macro definition with parameters

#define Macro Name (parameter) string

#define S (A, b) a*b

Area = S (A, b);

#define MAX (x, y) > (y)? (x) y)

The difference between 3.typedef and # define

Normally typedef because it handles pointer types correctly

typedef char *STRING1;

#define STRING2 char *

String1 s1,s2;

String2 S3,s4;

S1,S2,S3 is defined for char* but S4 is defined for char type

3. Static variables

Static variables are broadly divided into three uses

1. Used in a local variable to become a static local variable. Static local variables have two usages, memory functions, and global lifetimes.

2. For global variables, the primary role is to restrict this global variable from being called by other files.

3. Used for members in a class. Indicates that this member belongs to this class but does not belong to any particular object in the class

1. Static local Variables

Static local variables belong to static storage, which has the following characteristics:

(1) A 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.

(2) allows the static local amount of the construction class to be assigned an initial value such as an array, if the initial value is not assigned, then the system automatically assigns 0 values.

(3) If the static local variable of the basic type is not assigned the initial value in the description, the system automatically assigns 0 values. 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:

void Fun ()

{

static int a = 1;

a++;

}

The first time you enter this function, the variable A is initialized to 1! And then since the increment of 1, each time after entering the function, a will not be initialized again, only to increase the operation of 1; Before the static invention, to achieve the same function, you can only use global variables:

int a = 1;

void Fun ()

{

a++;

}

2. Static global variables

A static global variable is formed by adding static to the global variable (external variable). 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.

Class member variables for 3.static

The static keyword has two meanings, and you look at the context to determine

1. Indicates that the variable is a static storage variable, indicating that the variable is stored in a static storage area.

2.

Indicates 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).

Second, the static function--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.

1 intrinsic functions (also called static functions)

If a function defined in a source file can only be called by a function in this file, but 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 a "static" keyword before the function type, as follows:

static function type function name (function parameter table)

{......}

The keyword "static", translated into Chinese, is "still", so the internal function is also called the static function. But the meaning of "static" here does not mean storage, but 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.

2 external functions

Definition of an external function: when defining a function, if the keyword "static" is not added, or the keyword "extern" indicates that the function is an external function:

[extern] Function type function name (function parameter table)

{......}

When you call an external function, you need to describe it:

[extern] Function type function name (parameter type table) [, Function name 2 (parameter Type table 2) ...];
Technical Consulting: Shenzhen Xin Ying tatsu embedded, single-chip computer training beam teacher 18336308250 qq:2825915309

Some misuse and knowledge summarization of 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.