tags:

views:

333

answers:

3

I want to create a header file for C, C++ which is specific to help in randomization.

+1  A: 

I'm still not sure that I understand your question (I posted a comment to your question), but an answer might be this:

#include <stdlib.h> /* required for rand() and srand() */

This is the answer if you want to invoke the rand() and srand() functions, which are part of the C standard library. For example, see http://www.google.com/search?q=srand

ChrisW
Neither random() nor randomize() is part of the standard C library in C89 or C99. The standard functions are rand() and srand().
Jonathan Leffler
Thanks: I'll edit my post accordingly.
ChrisW
A: 

Try looking at http://www.daniweb.com/forums/thread1769.html, kasperasky, and see if it helps you to answer your question--or at least ask a little more specific question.

Onorio Catenacci
+3  A: 

Header files are C files, except that they shouldn't contain any code implementation (only declarations).

For example, suppose the C file "dummy.c":

int dummy_function(int x){
   return x+1;
}

A good C header file would be:

#ifndef _DUMMY_HEADER_H_
#define _DUMMY_HEADER_H_ //This helps solve some possible errors

int dummy_function(int x);

#endif

If you use structures as arguments (or return values), you should put their declaration in the header.

typedef struct { int value; } myStruct;
int dummy_function(myStruct* x){
   return (*x).value+1;
}

Instead, you should write

#include "dummy.h"
int dummy_function(myStruct* x){
   return (*x).value+1;
}

And create the following header file:

#ifndef _DUMMY_HEADER_H_
#define _DUMMY_HEADER_H_

typedef struct{ int value; } myStruct;
int dummy_function(myStruct* x);

#endif

A good header file should be valid by itself (include all the '#include' statements it needs).

So, if your code is the implementation of a "double myRandom()" function, you should write this header file:

#ifndef _HEADER_NAME_
#define _HEADER_NAME_

double myRandom(void);  /* Can omit void in C++, but not in C */

#endif

If this isn't your problem, then please specify better.

luiscubal
I use extern on function declarations (as well as data declarations), primarily for consistency between the two. Then, anything that is not a typedef (or enum type definition) or an extern declaration is suspect.
Jonathan Leffler