Tuesday 30 April 2013

Preprocessor in Windows


#if defined(_WIN64)
  // 64-bit programs run only on Win64
#elif defined(_WIN32)
 // 32-bit programs run on both 32-bit and 64-bit Windows
#else
 // Win64 does not support Win16
#endif
#ifdef _WIN64  
 
#else  
#endif
#define TRACE(n) printf("TRACE: %d\n", n)
the predefined ANSI C and Microsoft C++ implementation macros
__LINE__
__FILE__ 
__DATE__ 
__TIME__
Simple Example :
Example 1 :msdn.microsoft.com
#include <stdio.h>
#define  flag 1
#define  CHECK1(m,...) \
   if(flag == 1){\
   printf(m, __VA_ARGS__);\
   }

int main ()
{
  CHECK1("%s .\n","hello world");
  return 0;
}
                  hello world .

#define DEBUG(x) do { if (flag) { std::cerr << x; } } while (0)
Example 2 : debugging

#include <iostream>
using namespace std;
int main()
{
 #ifdef _DEBUG
 cout << "debug " << endl;
 #else
    cout<<"release "<<endl;
 #endif
 return 0;
}
project settings -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions . 
Example 3.  Preprocessor directives.
#define :
#include <iostream>
using namespace std;
#define test( a1, a2 ) ( a1 * a2 )
#define Max    100
#define SWAP(a, b)  do {int m =a ; a = b;b= m; } while ( 0 )
void swap(int a , int b)
{
 int m = a;
 a = b;
 b = m;
}
int main()
{
 int a = 10 ;
 int b = 20;
 SWAP(a, b);
 cout<<"call macro ."<<endl;
 cout<<"a  = " <<a <<endl;
 cout<<"b  = " <<b <<endl;
 a = 10;
        b = 20;
        swap(a , b);
 cout<<"call function ."<<endl;
 cout<<"a  = " <<a <<endl;
 cout<<"b  = " <<b <<endl;
 return 0;
}
call macro .
a  = 20
b  = 10
call function .
a  = 10
b  = 20
Press any key to continue . . .
Note : 

 Function and "#define"  preprocessor  are very  different  .
+ The "#define" is called  , compiler will copies  code . So when compiler  copies  code, size of code  will  increase,  But program will run   more quickly.
+ The function  is called , Compiler will accesses to  address of this  function .So When compiler accesses function , size of code will  not change , but program will run slowly.

This are some  normally errors about "#define "  preprocessor
#define square(x) x * x To  #define square(x) ((x) * (x))


  #include 
   #include <iostream>
   #include "MyFunction.h"
   #include "../FolderName/FolderName1/test.h"
   #pragma
                                                                                   
#if defined(UNICODE)
    #define _tcout wcout
#else
    #define _tcout cout
#endif

int _tmain(int argc, TCHAR* argv[])
{
    for (int c = 0; c < argc; c++)
    {
       _tcout << argv[c] << " ";
    }

    return 0
}                 
                                                                                ThongLT -   letrthong@gmail.com
http://c-faq.com/cpp/index.html
http://support.microsoft.com/kb/153901

No comments:

Post a Comment