Page 140 - C-Language
P. 140

Chapter 19: Declarations




        Remarks



        Declaration of identifier referring to object or function is often referred for short as simply a
        declaration of object or function.


        Examples



        Calling a function from another C file


        foo.h


         #ifndef FOO_DOT_H    /* This is an "include guard" */
         #define FOO_DOT_H    /* prevents the file from being included twice. */
                              /* Including a header file twice causes all kinds */
                              /* of interesting problems.*/

         /**
          * This is a function declaration.
          * It tells the compiler that the function exists somewhere.
          */
         void foo(int id, char *name);

         #endif /* FOO_DOT_H */


        foo.c


         #include "foo.h"    /* Always include the header file that declares something
                              * in the C file that defines it. This makes sure that the
                              * declaration and definition are always in-sync.  Put this
                              * header first in foo.c to ensure the header is self-contained.
                              */
         #include <stdio.h>

         /**
          * This is the function definition.
          * It is the actual body of the function which was declared elsewhere.
          */
         void foo(int id, char *name)
         {
             fprintf(stderr, "foo(%d, \"%s\");\n", id, name);
             /* This will print how foo was called to stderr - standard error.
              * e.g., foo(42, "Hi!") will print `foo(42, "Hi!")`
              */
         }


        main.c


         #include "foo.h"





        https://riptutorial.com/                                                                             116
   135   136   137   138   139   140   141   142   143   144   145