Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8255233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:27:17+00:00 2026-06-08T01:27:17+00:00

So I’m still getting used to modular programming, and want to make sure I’m

  • 0

So I’m still getting used to modular programming, and want to make sure I’m adhering to best practices. If I have the two module header files below, will the the headers #included by each file (for example “mpi.h”) be included multiple times? Is there a proper way to account for this?

Also, my module headers typically look like these examples, so any other criticism/pointers would be helpful.

/* foo.h */
#ifndef FOO_H
#define FOO_H

#include <stdlib.h>
#include "mpi.h"

void foo();

#endif

and

/* bar.h */
#ifndef BAR_H
#define BAR_H

#include <stdlib.h>
#include "mpi.h"

void bar();

#endif

And use the sample program:

/* ExampleClient.c */
#include <stdlib.h>
#include <stdio.h>
#include "mpi.h"
#include "foo.h"
#include "bar.h"

void main(int argc, char *argv[]) {
    foo();
    MPI_Func();
    bar();
    exit(0)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:27:20+00:00Added an answer on June 8, 2026 at 1:27 am

    What do you mean by ‘include’? The preprocessor statement #include file copies the contents of file and replaces the statement with these contents. This happens no matter

    If by ‘include’ you mean “the statements and symbols in these files will be parsed multiple times causing warnings and errors”, then no, the include guards will prevent that.

    If by ‘include’ you mean “some part of compiler will read some part of these files”, then yes, they’ll be included multiple times. The preprocessor will read the second inclusion of the file and replace it with a blank line because of the include guards, which incurs a tiny overhead (the file is already in memory). Modern compilers (GCC, not sure about others) will probably be optimized to avoid this, however, and note that the file has include guards on the first pass and simply discard future inclusions, removing the overhead – Don’t worry about speed here, clarity and modularity are more important. Compilation is a time-consuming process, for sure, but #include is the least of your worries.

    To better understand include guards, consider the following code sample:

    #ifndef INCLUDE_GUARD
    #define INCLUDE_GUARD
    // Define to 1 in first block
    #define GUARDED 1
    #endif
    
    #ifndef INCLUDE_GUARD
    #define INCLUDE_GUARD
    // Redefine to 2 in second block
    #define GUARDED 2
    #endif
    

    After (the first pass of) preprocessing, what will GUARDED be defined to? The preprocessor statement #ifndef or its equivalent, #if !defined() will return false if their argument is indeed defined. Therefore, we can conclude that the second #ifndef will return false, so only the first definition of GUARDED will remain after the first pass of the preprocessor. Any instance of GUARDED remaining in the program will be replaced by 1 on the next pass.

    In your example, you’ve got something slightly (but not much) more complicated. Expanding all the #include statements in ExampleClient.c will result in the following source: (Note: I indented it, but that’s not normal style for headers and the preprocessor won’t do it. I just wanted to make it more readable)

    /* ExampleClient.c */
    //#include <stdlib.h>
      #ifndef STDLIB_H
        #define STDLIB_H
        int abs (int number); //etc.
      #endif
    
    //#include <stdio.h>
      #ifndef STDLIB_H
        #define STDLIB_H
        #define NULL 0 //etc.
      #endif
    
    //#include "mpi.h"
      #ifndef MPI_H
        #define MPI_H
        void MPI_Func(void);
      #endif
    
    //#include "foo.h"
      #ifndef FOO_H
        #define FOO_H
        //#include <stdlib.h>
          #ifndef STDLIB_H
            #define STDLIB_H
            int abs (int number); //etc.
          #endif
        //#include "mpi.h"
          #ifndef MPI_H
            #define MPI_H
            void MPI_Func(void);
          #endif
        void foo(void);
      #endif
    
    
    //#include "bar.h"
      #ifndef BAR_H
        #define BAR_H
        //#include <stdlib.h>
          #ifndef STDLIB_H
            #define STDLIB_H
            int abs (int number); //etc.
          #endif
        //#include "mpi.h"
          #ifndef MPI_H
            #define MPI_H
            void MPI_Func(void);
          #endif
        void bar(void);
    #endif
    
    void main(int argc, char *argv[]) {
        foo();
        MPI_Func();
        bar();
        exit(0); // Added missing semicolon
    }
    

    Go through that code and note when various definitions are performed. The result is:

    #define STDLIB_H
    int abs (int number); //etc.
    #define STDLIB_H
    #define NULL 0 //etc.
    #define MPI_H
    void MPI_Func(void);
    #define FOO_H
    void foo(void);
    #define BAR_H
    void bar(void);
    

    With respect to your request for other criticism/pointers, why are you #including stdlib.h and mpi.h in all your headers? I understand that this is a stripped down example, but in general, header files should only include files necessary for the declaration of their contents. If you use a function from stdlib or call MPI_func() in foo.c or bar.c, but the function declarations are simply void foo(void), you shouldn’t include these files in the header function. For example, consider the following module:

    foo.h:

    #ifndef FOO_H
    #define FOO_H
    void foo(void);
    #endif
    

    foo.c:

    #include <stdlib.h>  // Defines type size_t
    #include "mpi.h"     // Declares function MPI_func()
    
    #include "foo.h"     // Include self so type definitions and function declarations
                         // in foo.h are available to all functions in foo.c
    
    void foo(void);
      size_t length;
      char msg[] = "Message";
    
      MPI_func(msg, length);
    }
    

    In this example, the implementation of foo() requires stuff from stdlib and mpi, but the definition does not. If foo() returned or required a size_t value (typedef’ed in stdlib), you’d need to #include stdlib in the .h file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.