Thread: Variable and functions declarations for header file

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    Variable and functions declarations for header file

    I have written code following code

    Code:
    #include<stdio.h>
    
    void Wait ( unsigned int size )
    {
    	unsigned int  i;
    	
    	for ( i = 0; i < size; i++)
    	{
    		printf(" Loop = %d \n", i);
    	}
    }
    int main (void)
    {
    	 Wait ( 2);
    	return 0;
    }
    I want to create header file header.h"

    Code:
    #include<stdio.h>#include"header.h"
    
    
    int main (void)
    {
    	 Wait ( 2);
    	return 0;
    }
    What should be written in header.h?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    If Wait() and main() are in the same file as you show in the first example above, then you don't need a header file. Yes, you could create a header file even if they are in the same file, in preparation to add more functions and more .c files.

    If Wait() is in a separate file, then the prototype (declaration) for Wait() should be placed in the header file, and #include in both files.

    If you print out an unsigned int, then use "%u" to display.

    foo.c
    Code:
    #include<stdio.h>
    #include"header.h"
     
    int main (void)
    {
        Wait ( 2);
        return 0;
    }
    file1.c
    Code:
    #include<stdio.h>
    #include"header.h"
    
    void Wait ( unsigned int size )
    {
        unsigned int  i;
         
        for ( i = 0; i < size; i++)
        {
            printf(" Loop = %u \n", i);
        }
    }
    header.h
    Code:
    void Wait ( unsigned int size );
    Last edited by rstanley; 09-03-2021 at 08:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tentative variable in header file problem
    By Cpt. Rambler in forum C Programming
    Replies: 2
    Last Post: 09-04-2015, 06:02 PM
  2. Replies: 2
    Last Post: 12-11-2014, 04:53 AM
  3. struct declarations in header files
    By nicoeschpiko in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 01:24 AM
  4. Putting functions in a header file
    By Vertex34 in forum C Programming
    Replies: 6
    Last Post: 09-29-2004, 02:33 PM
  5. knowing what functions are in a header file
    By nextus in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2002, 03:52 PM

Tags for this Thread