Thread: How to make separate header file from code

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    How to make separate header file from code

    Here is my example code :

    Code:
    #include <stdio.h>
    
    void Func() {
      static int x = 0;
      x++;
      printf("%d\n", x); 
    }
    
    
    int main() {
      Func();  // prints 1
      Func();  // prints 2
      Func();  // prints 3
      Func();  // prints 4
      Func();  // prints 5
    
    
      return 0;
    }
    I need help to create header file from my code

    Code:
    #include <stdio.h>#include "test.h"
    
    
    int main() {
      Func();  // prints 1
      Func();  // prints 2
      Func();  // prints 3
      Func();  // prints 4
      Func();  // prints 5
    
    
      return 0;
    }
    test.h

    Code:
    static int x ;
    //code for header
    I am bit confused I don't have any idea how to make header file test.h from the program

  2. #2
    Guest
    Guest
    Hello vajra11, you can just put your code into the header file as-is; it's as simple as that!¹

    test.h
    Code:
    #include <stdio.h>
    
    void Func() {
        static int x = 0;
        x++;
        printf("%d\n", x); 
    }
    main
    Code:
    #include "test.h"
    
    int main() {
        Func();  // prints 1
        Func();  // prints 2
        Func();  // prints 3
        Func();  // prints 4
        Func();  // prints 5
        return 0;
    }
    ¹ You'll want to learn about header-guards down the line, but for this simple example it doesn't matter.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by Guest View Post
    Hello vajra11, you can just put your code into the header file as-is; it's as simple as that!¹

    test.h
    Code:
    #include <stdio.h>
    
    void Func() {
        static int x = 0;
        x++;
        printf("%d\n", x); 
    }
    main
    Code:
    #include "test.h"
    
    int main() {
        Func();  // prints 1
        Func();  // prints 2
        Func();  // prints 3
        Func();  // prints 4
        Func();  // prints 5
        return 0;
    }
    ¹ You'll want to learn about header-guards down the line, but for this simple example it doesn't matter.
    I hope you're not serious

  4. #4
    Guest
    Guest
    I beg your pardon?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hodor means that if you include that .h file in more than one place, you end up with multiply defined symbols.
    Whilst technically correct for a single source file example, it's an awful example to show people what to do.

    And no, header guards won't fix that problem.

    test.h
    Code:
    #ifndef TEST_H_INCLUDED
    #define TEST_H_INCLUDED
    void Func();
    #endif
    test.c
    Code:
    #include "test.h"
    #include <stdio.h>
     
    void Func() {
        static int x = 0;
        x++;
        printf("%d\n", x); 
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Guest
    Guest
    Alright, point taken. I just wanted to give them the simplest workable solution for test.h, since they didn't ask about source files.

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Hodor means that if you include that .h file in more than one place, you end up with multiply defined symbols.
    Whilst technically correct for a single source file example, it's an awful example to show people what to do.

    And no, header guards won't fix that problem.

    test.h
    Code:
    #ifndef TEST_H_INCLUDED
    #define TEST_H_INCLUDED
    void Func();
    #endif
    test.c
    Code:
    #include "test.h"
    #include <stdio.h>
     
    void Func() {
        static int x = 0;
        x++;
        printf("%d\n", x); 
    }
    There is no main function in your code It will never run

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vajra11 View Post
    There is no main function in your code It will never run
    He his using the same main.c file as the prior poster.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by stahta01 View Post
    He his using the same main.c file as the prior poster.

    Tim S.
    I do not understand what you mean

    test.h
    Code:
    #ifndef TEST_H_INCLUDED#define TEST_H_INCLUDED
    void Func();
    #endif
    test.c
    Code:
    #include <stdio.h>#include "test.h"
    
    
      
    void Func() {
        static int x = 0;
        x++;
        printf("%d\n", x); 
    }
    main.c
    Code:
    #include<stdio.h>#include "test.h"
      
    int main() {
        Func();  // prints 1
        Func();  // prints 2
        Func();  // prints 3
        Func();  // prints 4
        Func();  // prints 5
        return 0;
    }

    The output is as follows

    1
    2
    3
    4
    5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2016, 07:30 PM
  2. Replies: 5
    Last Post: 07-06-2013, 08:22 AM
  3. How do you make a header file
    By Inkinsarto in forum C++ Programming
    Replies: 12
    Last Post: 05-03-2013, 07:33 AM
  4. How to make a Library file and header file in GCC?
    By mhrsolanki2020 in forum C Programming
    Replies: 16
    Last Post: 12-22-2012, 11:51 PM
  5. I want to make a *.lib for my header file
    By tudehopet in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2002, 04:14 AM

Tags for this Thread