CMPC
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Source and Header Files

4 posters

CMPC :: Computing :: Programming :: C++

Page 1 of 2 1, 2  Next

Go down

Source and Header Files Empty Source and Header Files

Post by Admin Wed Dec 30, 2009 10:43 am

Header Files

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.
- quoted from Wikipedia.





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
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Wed Dec 30, 2009 10:55 am

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.


Last edited by another_pd on Wed Dec 30, 2009 11:04 am; edited 1 time in total
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Wed Dec 30, 2009 10:59 am

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
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Wed Dec 30, 2009 12:14 pm

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)

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.
As well as. . .
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 Wink Very Happy 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 Wink

Random number (100,300) : 126
heh, heh, random number between 100 and 300 inclusive (of at least 100 maybe not 300)


Last edited by another_pd on Thu Dec 31, 2009 12:03 am; edited 3 times in total
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Wed Dec 30, 2009 1:10 pm

study I see. Also paul, you're an asshole.


Also I didn't steal the gravity code dumbass.
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Wed Dec 30, 2009 1:19 pm

here you go paul


let's see you be smart now you jackass


Source and Header Files Integral7

Source and Header Files CalculusFormulas01

Source and Header Files Calculus_integral
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Wed Dec 30, 2009 9:49 pm

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 Cool 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
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Wed Dec 30, 2009 9:51 pm

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.
Corey
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Wed Dec 30, 2009 11:46 pm

Admin wrote:study I see. Also paul, you're an asshole.


Also I didn't steal the gravity code dumbass.
I never said you stole it. . .
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Thu Dec 31, 2009 12:11 am

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 Suspect
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Thu Dec 31, 2009 12:51 am

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. . .
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Thu Dec 31, 2009 1:01 am

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. . .
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Thu Dec 31, 2009 1:03 am

would it be the rpg game you showed me at the beginning of the year? with the hatchet alien
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Thu Dec 31, 2009 1:06 am

I missed keeping track of mine at the beginnning... I guess it won't do much good now...
Corey
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Thu Dec 31, 2009 2:26 am

takuroindigo wrote:I missed keeping track of mine at the beginnning... I guess it won't do much good now...
estimate the time you've already done . . . then keep track
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Thu Dec 31, 2009 2:27 am

Admin wrote:would it be the rpg game you showed me at the beginning of the year? with the hatchet alien
Yes, it would be btw: I noticed that my moderator powers disappeared overnight Wink
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Thu Dec 31, 2009 2:42 am

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
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Thu Dec 31, 2009 3:53 am

I'm so bad at estimating....
Corey
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Paul Thu Dec 31, 2009 4:21 am

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. . .
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Thu Dec 31, 2009 4:30 am

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
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Thu Dec 31, 2009 4:42 am

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 Razz eh weird, a forum that doesn't need mods.
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Thu Dec 31, 2009 4:42 am

but i guess it's because we only have 11 registered users. wait a few years and we'll need em
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Thu Dec 31, 2009 5:52 am

Won't you be graduated in a few years?
Corey
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Admin Thu Dec 31, 2009 5:55 am

your point?
Admin
Admin
Pirate King

Posts : 559

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Corey Thu Dec 31, 2009 6:03 am

I dunno... Just pointing it out...
Corey
Corey
Mod

Posts : 462

Back to top Go down

Source and Header Files Empty Re: Source and Header Files

Post by Sponsored content


Sponsored content


Back to top Go down

Page 1 of 2 1, 2  Next

Back to top

- Similar topics

CMPC :: Computing :: Programming :: C++

 
Permissions in this forum:
You cannot reply to topics in this forum