32 meaning of function

catalogue

1 functions in C language

  • Origin of function
    • Program = data + algorithm
    • C program = data + function

2 meaning of function

  • Modular programming

  • Modularization in C language

3 process oriented programming

  • Process oriented programming is a process centered programming idea
  • Firstly, complex problems are decomposed into easy to solve problems
  • The decomposed problems can be completed step by step
  • Function is the embodiment of process oriented in C language
  • Each step of solving the problem can be realized by function

4 declarations and definitions

  • The declaration and definition are not the same

  • The meaning of the declaration is to tell the compiler the existence of the program unit

  • The definition clearly indicates the meaning of the program unit

  • Declaration of program units through extern in C language

  • Some program units can be declared without extern

  • Example: difference between declaration and definition

    • Demo1

      // test.c
      #include <stdio.h>
      #include <malloc.h>
      
      //Declare external global variables, which are defined in other C files. The compiler will not give g here_ Var allocation space
      extern int g_var;
      
      //Declare the external global structure, which is defined in other C files
      extern struct Test;
      
      int main()
      {
          //Declare functions that are defined in other C files
          extern void f(int i, int j);
          extern int g(int x);
          
          struct Test* p = NULL; // (struct Test*)malloc(sizeof(struct Test));
          
          printf("p = %p\n", p);
          
          g_var = 10;
          
          printf("g_var = %d\n", g_var);
          
          f(1, 2);
          
          printf("g(3) = %d\n", g(3));
          
          free(p);
          
          return 0;
      }
      
      
      //global.c
      #include <stdio.h>
      
      int g_var = 5;
      
      struct Test
      {
          int x;
          int y;
      };
      
      void f(int i, int j)
      {
          printf("i + j = %d\n", i + j);
      }
      
      int g(int x)
      {
          return (int)(2 * x + g_var);
      }
      
    • Compilation: struct does not need to be declared with extern

      test.c:6: warning: useless storage class specifier in empty declaration
      
    • function

      p = (nil)
      g_var = 5
      g_var = 10
      i + j = 3
      g(3) = 16
      
    • Demo2: modify the memory allocation method pointed by the p pointer

      // test.c
      #include <stdio.h>
      #include <malloc.h>
      
      extern int g_var;
      
      extern struct Test;
      
      int main()
      {
          extern void f(int i, int j);
          extern int g(int x);
          
          struct Test* p = (struct Test*)malloc(sizeof(struct Test));
          
          printf("p = %p\n", p);
          
          g_var = 10;
          
          printf("g_var = %d\n", g_var);
          
          f(1, 2);
          
          printf("g(3) = %d\n", g(3));
          
          free(p);
          
          return 0;
      }
      
      
      //global.c
      #include <stdio.h>
      
      int g_var = 5;
      
      struct Test
      {
          int x;
          int y;
      };
      
      void f(int i, int j)
      {
          printf("i + j = %d\n", i + j);
      }
      
      int g(int x)
      {
          return (int)(2 * x + g_var);
      }
      
    • compile

      • Analysis: if the order in which the compiler compiles files is uncertain, the compiler is compiling Test C file, the size information of the structure Test cannot be known when compiling to line 14
      test.c: In function 'main':
      test.c:13: error: invalid application of 'sizeof' to incomplete type 'struct Test' 
      
    • Demo3: g_ The types of VaR variables are different when declared and defined

      // test.c
      #include <stdio.h>
      #include <malloc.h>
      
      extern int g_var;
      
      extern struct Test;
      
      int main()
      {
          extern void f(int i, int j);
          extern int g(int x);
          
          struct Test* p = NULL;
          
          printf("p = %p\n", p);
          
          g_var = 10;
          
          printf("g_var = %d\n", g_var);
          
          f(1, 2);
          
          printf("g(3) = %d\n", g(3));
          
          free(p);
          
          return 0;
      }
      
      
      //global.c
      #include <stdio.h>
      
      float g_var = 5;
      
      struct Test
      {
          int x;
          int y;
      };
      
      void f(int i, int j)
      {
          printf("i + j = %d\n", i + j);
      }
      
      int g(int x)
      {
          return (int)(2 * x + g_var);
      }
      
    • Compile run: variable g_var prints out a different value than expected

      • Analysis: the compiler is processing test In line 19 of the C file, it is stored to G according to the storage method of int_ The memory value corresponding to VaR, that is, a binary float value is interpreted as int
      p = (nil)
      g_var = 1084227584
      g_var = 10
      i + j = 3
      g(3) = 6
      

Tags: C

Posted by BruceRowe on Thu, 12 May 2022 17:22:06 +0300