C
It is a tool that improves efficiency. That is why we have to spend more time and experience after familiar with the previous language. Maybe we will decrease within a period of time, but we will get better efficiency after we master new tools. In C , the only way to greatly improve productivity is to use --- library. This is also one of the main goals of C design. It is aware of C 's data definition and design, and you will have a more in-depth understanding of the use library.
First, you must know the difference between "declaration" and "definition".
Whether it is a variable or a function, "declaration" introduces the meaning of the name to the computer; and "definition" allocates storage space for this name. When declaring, use the extern keyword, the extern keyword of the function is optional. A function that does not have a function is automatically configured to be a statement. (Note: The function in C must be declared before defining, that is, there is a function prototype, which is the main purpose of making the compiler to check.) Here are some specific examples:
// EXAMPLE for C Declaration and Definition
Extern int A; // Variable declaration without definition
Declaration of Extern Int i (int) // function
INT B; // Variable declaration is defined
Definition of INT i (int x) // function
{
Return X 1;
}
Void main (void)
{A = 1;
i (a);
}
The first step of C to C will start from the relationship between functions and variables. I believe that those who are familiar with C language know that if they are used to use a set of different types of variables together, they will use the struct key. There is also a set of functions that you want to cooperate for calls to achieve the different functions of the STRUCT. But one thing, that is, all these functions are also visible to the Struct, which can call this function. This is unsafe, there is nothing benefit to the modularization of the program design.
The first step in C is to put the function in Struct, as a member function. This brings a new problem: the function in each structure can be renamed. When defining this function, it needs to be fully specified which one is. In order to complete this specified task, C has a new operator::, ie the scope breakdown operator (used to specify the execution range of the function, which Struct).
In C , the function is placed in a root change in C , and let us think of the structure as a new concept. In C, the structure is aggregation of data, which bundles the data together so that we can view them as a package. But in addition to making the programming, there is no other benefit. These structures can be used elsewhere. However, the function is also placed in this package, and the structure becomes new creation, which can describe the properties (just like Struct in C), and describe behavior, which forms the concept of objects. . Object is an independent entity that has its own memory and event. In C , the object is just a variable, and the most pure definition is "one area stored". It is a space that can store data and implies the operations on these data. Bringing the data together with a function, this allows you to create a new type. This is often referred to as package.
However, we will see that the method of completing the operation on the object is different.
Object.member_function (arglist) is a "call a member function" for an object. In the object-oriented usage, it is also called "send a message to an object". In this way, for Stash S, the statement S.Add (& i) "Send a message to S", "That is," for you Add () ". In fact, object-oriented programming can be summarized as a sentence, "send messages to objects". Everything you need to do is to create a bundle object and send them a message. Of course, the problem is to sket out what our objects and messages are, but if these, the implementation of C is straightforward. The size of a structure is all of its membership. Sometimes, when an Structure is processed by a compiler, additional bytes will be added to make the bundle more tie, mainly in order to improve the performance efficiency.
Use SIZEOF to calculate the Struct's memory size.
// size of struct
#include
Struct a
{
INT i [100];
}
Struct B
{
Void x ();
}
Void main (void)
{
Printf ("SIZE OF STRUCT A IP% D / N", SIZEOF (A);
Printf ("SIZE OF STRUCT B IS% D / N", SIZEOF (B);
}
The first PRINTF prints 200, and the second sentence will print an uncertain non-zero value. This is not legal in C, but it is legal in C .
In this article, I introduced the basic method of using C , that is, put the function in the internal placement. This new type is called an abstract data type, and variables created with this structure are called this type of object or instance. Calling member functions to the object call to this object. The main activity in object-oriented programming is to send messages to objects. Although the data and functions are tied together, it is more likely to use, because this can prevent the name conflict by hidden names, but there is a lot of work that can make C programming
The author is small, I hope to have some opinions, please Mail, I: Paulni@citiz.net