Source and Header Files
4 posters
CMPC :: Computing :: Programming :: C++
Page 1 of 2
Page 1 of 2 • 1, 2
Source and Header Files
Header Files
- quoted from Wikipedia.In computer science, header files are a feature of some programming languages (most famously C and C++) that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required. The C standard library and C++ standard library traditionally declare their standard functions in header files.
Source Files
eh these don't need such an elaborate definition. It's basically the file that you write your regular stuff in such as the main file. Header files compliment source files.
_____
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
It's actually more complicated than it would seem. . .
I mean it would seem fairly straight forward, but after all you can include not only header files, but source files as well. . .
Basically, linking and header and source files are the main reason people use IDE's, It's cause they deal with a lot of the (somewhat) more complicated stuff.
I mean it would seem fairly straight forward, but after all you can include not only header files, but source files as well. . .
Basically, linking and header and source files are the main reason people use IDE's, It's cause they deal with a lot of the (somewhat) more complicated stuff.
Last edited by another_pd on Wed Dec 30, 2009 11:04 am; edited 1 time in total
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
hey can you explain to me why people sometimes write #ifndef, #define etc in the start of their files?
After reading about it I still don't fully understand
After reading about it I still don't fully understand
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
Admin wrote:hey can you explain to me why people sometimes write #ifndef, #define etc in the start of their files?
After reading about it I still don't fully understand
Allen, do you know that #include isn't actually C or C++ SOURCE code, but that it's actually PREPROCESSER DIRECTIVE
Ahw _ _ _ _(your choice of four lettered words, no not THAT four letter word. . .), I'm just going to (somewhat) imitate Allen and qoute from not only Wikipedia, but also from HIS TUTORIAL SITE. . . (The one he said he learned from)
As well as. . .Wikipedia wrote:
The C preprocessor (cpp) is the preprocessor for the C programming language. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). The language of preprocessor directives is agnostic to the grammar of C, so the C preprocessor can also be used independently to process other types of files.
The transformations it makes on its input form the first four of C's so-called Phases of Translation. Though an implementation may choose to perform some or all phases simultaneously, it must behave as if it performed them one-by-one in order.
Wikipedia wrote:
Conditional compilation
The #if, #ifdef, #ifndef, #else, #elif and #endif directives can be used for conditional compilation.
#ifdef _WIN32 // _WIN32 is defined by all Windows 32 compilers, but not by others.
#include <windows.h>
#else
#include <unistd.h>
#endif
#if VERBOSE >= 2
print("trace message");
#endif
Some compilers targeting Windows define WIN32, but all should define _WIN32[1] This allows code, including preprocessor commands, to compile only when targeting windows systems. Alternatively, the macro WIN32 could be defined implicitly by the compiler, or specified on the compiler's command line, perhaps to control compilation of the program from a makefile.
The example code tests if a macro _WIN32 is defined. If it is, the file <windows.h> is included, otherwise <unistd.h>.
A more complex example might be something like
#if !defined(WIN32) || defined(__MINGW32__) // non windows or mingw compiler
...
#endif
Macro definition and expansion
There are two types of macros, object-like and function-like. Object-like macros do not take parameters; function-like macros do. The generic syntax for declaring an identifier as a macro of each type is, respectively,
#define <identifier> <replacement token list>
#define <identifier>(<parameter list>) <replacement token list>
Note that the function-like macro declaration must not have any whitespace between the identifier and the first, opening, parenthesis. If whitespace is present, the macro will be interpreted as object-like with everything starting from the first parenthesis added to the token list.
Whenever the identifier appears in the source code it is replaced with the replacement token list, which can be empty. For an identifier declared to be a function-like macro, it is only replaced when the following token is also a left parenthesis that begins the argument list of the macro invocation. The exact procedure followed for expansion of function-like macros with arguments is subtle.
Object-like macros were conventionally used as part of good programming practice to create symbolic names for constants, e.g.
#define PI 3.14159
... instead of hard-coding those numbers throughout one's code. An alternative in both C and C++ is to apply the const qualifier to a global variable.
An example of a function-like macro is:
#define RADTODEG(x) ((x) * 57.29578)
This defines a radians to degrees conversion which can be written subsequently, e.g. RADTODEG(34) or RADTODEG (34). This is expanded in-place, so the caller does not need to litter copies of the multiplication constant all over his code. The macro here is written as all uppercase to emphasize that it is a macro, not a compiled function.
Look Allen, basically there is a program called a preprocessor. This program is run either before the compiler or by the compiler. Programmers use special lines of code to communicate with the preprocessor. These lines of code in C and C++ begin with a hash or pound sign (#). The #include preprocessor directive tells the preprocessor to include a file. The #define preprocessor directive tells the preprocessor to replace all instances of for example PI with 3.1415962 (I hope I remember the digits of PI right)
(btw: Allen, For your "gravity challenge" you should use #define for G and if anyone has a better explanation than my up til 3:50 A.M. three times in a row one (meaning the above paragraph) please feel free to help explain to the programming club 'admin')
Oh yeah, I forgot the quote from HIS TUTORIAL SITE
Ehh. . . I'll just link to it: http://www.cplusplus.com/doc/tutorial/preprocessor/
btw: apparently, I enjoy being more vicious when I'm tired or maybe it's just that I'm more like Allen, anyways. . .
Oh and also, yes I saw that you said you didn't understand AFTER reading about it. I give you credit for admitting that. One of the first steps towards becoming a better programmer is figuring out what you don't know. . . and then to admit it
- Random number (100,300) : 126
Last edited by another_pd on Thu Dec 31, 2009 12:03 am; edited 3 times in total
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
I see. Also paul, you're an asshole.
Also I didn't steal the gravity code dumbass.
Also I didn't steal the gravity code dumbass.
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
here you go paul
let's see you be smart now you jackass
let's see you be smart now you jackass
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
FreeBASIC has those too. Called preprocessor keywords. You can use #define to tell the compiler to replace a keyword with another keyword. Ex. #define version 0.1 would tell the compiler to substitute every instance of the word version with "0.1" You can also substitute things with commands. ex. #define red rgb(255,0,0) or #define hi print "hello world"
etc.
You can then use things like #ifdef, #ifndef, and #if/#else/#endif
ex.
#define debug_mode
#ifdef debug_mode
do stuff
#end if
ex.
#define DEBUGGING
#define FALSE 0
#define TRUE NOT FALSE
#define MyRGB(R,G,B) (((R)SHL 16) OR ((G)SHL OR (B))
...
#define DEBUG_LEVEL 1
#IF (DEBUG_LEVEL >= 2)
do stuff
#ENDIF
...
This next one is useful if you're #including things:
#ifndef stuff
#define stuff
#endif
etc.
You can then use things like #ifdef, #ifndef, and #if/#else/#endif
ex.
#define debug_mode
#ifdef debug_mode
do stuff
#end if
ex.
#define DEBUGGING
#define FALSE 0
#define TRUE NOT FALSE
#define MyRGB(R,G,B) (((R)SHL 16) OR ((G)SHL OR (B))
...
#define DEBUG_LEVEL 1
#IF (DEBUG_LEVEL >= 2)
do stuff
#ENDIF
...
This next one is useful if you're #including things:
#ifndef stuff
#define stuff
#endif
Corey- Mod
- Posts : 462
Re: Source and Header Files
Using pre-processor commands such as
#define PI 3.141592653
#define 2PI 6.283185306
etc. saves memory because all instances of PI are replaced before compilation.
#define PI 3.141592653
#define 2PI 6.283185306
etc. saves memory because all instances of PI are replaced before compilation.
Corey- Mod
- Posts : 462
Re: Source and Header Files
I never said you stole it. . .Admin wrote: I see. Also paul, you're an asshole.
Also I didn't steal the gravity code dumbass.
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
unless i'm crazy (there's a possibility) i'm pretty sure you did write that. you even edited the post 3 times so you might have changed it just to screw with me
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
I had a few spelling errors I saw. . . And I probably have quite a few more. . .
I'm pretty sure your crazy. . .
although that wouldnt be what had proved it to me. . .
I'm pretty sure your crazy. . .
although that wouldnt be what had proved it to me. . .
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
I finally found the txt file i kept track of the time I spent on my game in. . .
hm. . .
19:13 so far. . .
considering that I'm pretty much considering rewriting most of the main code that will probably go up much much higher. . .
hm. . .
19:13 so far. . .
considering that I'm pretty much considering rewriting most of the main code that will probably go up much much higher. . .
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
would it be the rpg game you showed me at the beginning of the year? with the hatchet
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
I missed keeping track of mine at the beginnning... I guess it won't do much good now...
Corey- Mod
- Posts : 462
Re: Source and Header Files
estimate the time you've already done . . . then keep tracktakuroindigo wrote:I missed keeping track of mine at the beginnning... I guess it won't do much good now...
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
Yes, it would be btw: I noticed that my moderator powers disappeared overnightAdmin wrote:would it be the rpg game you showed me at the beginning of the year? with the hatchet
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
yeah i temporarily got pissed because of https://cmpc.forumotion.com/c-f1/source-and-header-files-t110.htm
hey howcome you don't really do anything moderator-like anyway? powers regiven
hey howcome you don't really do anything moderator-like anyway? powers regiven
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
He he. . .
other than deleting that AVACS FREE post I haven't really done much "mod" like but there hasn't been much occasion to. . .
other than deleting that AVACS FREE post I haven't really done much "mod" like but there hasn't been much occasion to. . .
Paul- Pickaxe
- Posts : 611
Re: Source and Header Files
Yeah... There's been nothing for me to mod either. We don't get many bad people on this forum... (mostly because Brad doesn't go to it...) heheheh Cunstoid
Corey- Mod
- Posts : 462
Re: Source and Header Files
lol Cunstoid . yup that's still funny
do we even need a rules thread? i can't think of any rules to put on the forum eh weird, a forum that doesn't need mods.
do we even need a rules thread? i can't think of any rules to put on the forum eh weird, a forum that doesn't need mods.
Admin- Pirate King
- Posts : 559
Re: Source and Header Files
but i guess it's because we only have 11 registered users. wait a few years and we'll need em
Admin- Pirate King
- Posts : 559
Page 1 of 2 • 1, 2
Similar topics
» Commercial/Open Source Games
» Null files
» Be aware of encrypted files
» Multiple Files are easy to follow
» Null files
» Be aware of encrypted files
» Multiple Files are easy to follow
CMPC :: Computing :: Programming :: C++
Page 1 of 2
Permissions in this forum:
You cannot reply to topics in this forum