80 likes | 92 Views
Learn how pre-compiler processes code, use macros, avoid errors, and interpret messages in C programming. Master #define, #ifdef, #ifndef, and more.
E N D
PreCompiler • Before every program is completed, a pre-compiler processes the code. • At this point the header files are included and linked • This is also the time when macros are executed. • All precompiler commands begin with a #
Macros • #define BIG 123 • int arr[BIG]; • In the temperory file, the preprocessor creates, the code is changed to • int arr[123]; • DO NOT be confused about using #defines as constants • The #define function can be used to replace one token directly with another
Macros contd… • Another way to use #define is to simply state that a variable is #defined • #define BIG • Later, you can test whether BIG has been defined and action is taken accordingly. • #ifdef evaluates to be true, if the string you have declared has already been defined. • #endif REMEMBER TO END YOUR IF STATEMENTS
Inclusion Guards • Your own CPP file is compiled into an OBJ file and the obj is then linked to the various header files using the linker. • Often header files that you create will use one another. • This means that you risk including files twice. • This leads to compiler errors when compiling • So we can use #ifndef as a valid way of checking for these • #ifndef OGL • #define OGL • ….. • #endif
Some Do’s and Don’ts • Do use conditional compilation when you need to create more than one version your code at the same time • Do use #undef to not let stray definitions in your project • Don’t let your compilations get too complex
Declaring Variables • HDC hDC=NULL; HGLRC hRC=NULL; HWND hWnd=NULL; HINSTANCE hInstance; • bool keys[256]; • bool active=TRUE; • bool fullscreen=TRUE;
Messages The Program interacts with the Operating System using a series of messages. Usually all the processing of these messages is done internally MSG is the structure (type) that is used for storing these messages It is the program that has control, as to whether it wants to read a message or not To see if there is a message waiting we use the following function PeekMessage(&msg,NULL,0,0,PM_REMOVE) The first parameter is to the structure that we declared, where we want the messages to be passed. This is passed as a pointer in the internal code The next three are irrelevant to any projects that we do in class The last parameter specifies what do to with the message once it has been processed. PM_remove removes the message from the queue.
GetMessage • MSG m; • m.message == WM_QUIT; is one valid check for a special quit message • This comes when the program uses the PostQuitMessage(0); • In all other cases, we use the TranslateMessage(&msg) to translate the message • And dispatch it to windows with the DispatchMessage(&msg). Usually windows then adds this message and calls any callbacks that we have defined.