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

  • Home
  • SEARCH
  • 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 4235558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:30:56+00:00 2026-05-21T02:30:56+00:00

I am used to Objective C header files and am not sure how C

  • 0

I am used to Objective C header files and am not sure how C header files are used in terms of good practice.

Where would one #include other source files, in the header file or the .c file?

Does the same idea apply to C where .c files include their own header files. and other files include the .h files of the source they want to include?

Is there anything equivalent to the @class usage in Objective-C?

Is it good practice to declare pointers in the .h file and initialize them/alloc them in the .c file?

  • 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-05-21T02:30:56+00:00Added an answer on May 21, 2026 at 2:30 am

    You normally distinguish between source and header files in the same way that Objective-C differentiates between implementation (.m) and interface (.h) files. Source files contain everything that may execute, header files contain enough information about symbols that other source files know how to communicate with that source file.

    Header files often include other header files, so you’ll see #include in both source and implementation files. #include operates exactly like #import except that it doesn’t automatically check whether you’ve #included the same file twice. So C header files often look something like:

    #ifndef __SOME_SYMBOL
    #define __SOME_SYMBOL
    
        ... rest of header file here ...
    
    #endif
    

    Which has the same effect of ensuring the main body of the header file is included only once.

    EDIT: more on this, as per request. Obviously you’d never do something like:

    #include "File.h"
    #include "File.h"
    

    But you can easily end up with something like:

    #include "FirstComplexThing.h"
    #include "SecondComplexThing.h"
    

    Where both FirstComplexThing.h and SecondComplexThing.h rely on something inside and hence #include SimpleThing.h. So you end up with SimpleThing.h #included twice, without making any sort of error or following any bad design pattern.

    C compilers work just like Objective-C compilers — each source file is compiled on its own, in isolation, with no overview until the linker comes along. #include is a preprocessor directive that has the same logical effect as copying the contents of the named file and pasting them into your source file at that location, so if you end up the same file #included twice you’ll probably end up with something like:

    char *somePointer; // I'm from SimpleThing.h
    
    ... lots of other things ...
    
    char *somePointer; // I'm from SimpleThing.h
    

    And the compiler will stop with an error that the same thing is declared twice. #import in Objective-C avoids that by being shorthand for ‘#include, but only if you haven’t already #included that file’. The C #ifndef/#define/#endif convention achieves the same thing as #import because the #ifndef/#endif pair say that the stuff in between should be passed on to the compiler if the nominated preprocessor symbol (__SOME_SYMBOL in my example; it tends to be a name derived from the name of that header file but exact conventions vary) hasn’t been defined. It won’t have been the first time the construct is encountered. Because it is defined inside the construct, it will have been when the same #ifndef is encountered the second time, so the stuff up to the matching #endif won’t be passed on.

    Although it’s a question of style, it is very often the case that each C file has one H file that is directly connected to it.

    There are no classes in C, obviously, but if you mean a construct like:

    @class SomeClass;
    
    @interface SomeOtherClass: NSObject
    {
          SomeClass *otherClass; // I can reference SomeClass without importing
                                 // the interface file because I've declared the
                                 // type above
    }
    
    - (void)whatever;
    @end
    

    That’s actually the normal C distinction between declarations and definitions. You’ll have a problem if you do something like:

    struct SomeStruct;
    
    struct SomeOtherStruct
    {
        struct SomeStruct otherStruct;
    };
    

    Because the compiler doesn’t have enough information. It doesn’t know how large SomeStruct should be, so it can’t work out how SomeOtherStruct should be laid out. However, this is completely valid:

    struct SomeStruct;
    
    struct SomeOtherStruct
    {
        struct SomeStruct *otherStruct;
    };
    

    Because the size of a pointer is always known, irrespective of what it is pointing to. You’ll often see that C libraries with opaque types describe those types by pointer only (sometimes to void *, but not always — e.g. stdio.h uses FILE *) or just give you an integer (including OpenGL, notably). So they ensure you’ve something that the compiler will know the size of without having to tell you what data they’re associating with it or giving you any way to try to manipulate it yourself.

    It’s perfectly good practice to put pointers in the header file (assuming it’s good practice to expose the thing globally, obviously). The same thing is often done in Objective-C, albeit for slightly different reasons, e.g.

    // interface/header file
    
    extern NSString *someGlobalIdentifier;
    

    And:

    // implementation/source file
    
    NSString *someGlobalIdentifier = @"somethingOrOther";
    

    In Objective-C that’s because you can then test identity rather than always having to test equality, but basically the same rules apply to C with respect to it being normal to put the reference (be it a pointer or whatever) that represents a thing into the header and create or declare the thing in a source file. In fact, if you start putting declarations in the header file you’ll end up with errors when the program comes to link because multiple source files will think they declare the thing.

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

Sidebar

Related Questions

Objective: I have a list of header files (about 50 of them), And each
I'm learning objective-c and keep bumping into the @ symbol. It is used in
I'm trying to debug an objective C program. It used to run, and I'm
I used to work in a place where a common practice was to use
We used to use SourceSafe, and one thing I liked about it was that
I'm used to using Objective-C protocols in my code; they're incredible for a lot
I have declared a header file which is giving Syntax error before '{' token.
One of the things I've been struggling with, whilst breaking into Objective-C programming, is
Do Nib files automatically initialize certain properties? In most objective-c code I've seen, you
I have this userInputstring in the header that will be modified and used by

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.