"C Language Learning" encapsulation and modular Thinking

Source: Internet
Author: User
"C language learning" encapsulation and modular thinking

2014-7-6 Reading 1162 Comments 2

Just after learning C, do the course design of C in a source file put hundreds of lines of code, and each function is interdependent, so it will be very troublesome. Because when I want to modify a place, it will be implicated in the need to modify the place to drink more. But in the actual program design, this is also undesirable. So the idea of modularity and encapsulation is important ... ★static Variable

a significant function of a static variable is the realization of a module's encapsulation.

The attributes of the static storage category determine that the global variable of the static declaration can only be referenced by a function of the source file. When a static global variable is defined in a source file, the other file cannot refer to the global variable by using an "extern" keyword, and can only access the global variable through the interface functions provided by the source file . ★ Example

I wrote a simple program to implement the Yang Hui's triangle. Where a static global variable array a[100][100] is defined, and then the following function is some action on the array. Functions other than the source file can only be used to complete the operation of the array by calling these functions.

Pascal_triangle.c

#include <stdio.h>

static int a[100][100];

void pascal_triangle (int num)
{
    int i,j;

    for (i = 1; I <= num i + +)
    {
        a[i][0] = 1;
        A[I][I-1] = 1;
    }

    for (i = 2;i < num. i + +)
    {for
        (j = 0; J < I-1; J + +)
        {
            a[i + 1][j + 1] = A[i][j] + a[i][j + 1];
        }
    }

    for (i = 1; I <= num;i + +)
    {
        for (j = 0; j < Num; j + +)
        {
            printf ("%d", a[i][j]);
            if (i = = (j + 1))
            {
                printf ("\ n");
                Break;}}}

Pascal_triangle.h

#ifndef pascal_triangle_h_included
#define pascal_triangle_h_included

extern void pascal_triangle (int num);

#endif//pascal_triangle_h_included
Main.c

#include <stdio.h>
#include "pascal_triangle.h"

int main (void)
{
    int num;
    printf ("Please enter data: \ n");
    scanf ("%d", &num);
    Pascal_triangle (num);
    return 0;
}
This is done. The implementation procedure of the Yang Hui's triangle seals the outside worlda function in another source file in a project does not need to understand how the specific operation of Yang Hui's trigonometric functions is carried out, just use the interface function provided in the module.。 So the implementation module of Yang Hui's triangle is completely independent of other modules. As shown in the figure:

The pascal_triangle.h header file is a statement that provides an external interface function to implement the Yang Hui's triangle, which is important.

When a source file is handled this way, the implementation function of the Yang Hui's triangle is modified anyway, without any effect on the other code. But one thing to note: Do not modify the function interface itself,

because once it's changed, other code could be a big change.

http://www.cnblogs.com/zhaoli/p/4176618.html

http://m.blog.csdn.net/blog/XGsilence/37091831

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.