Tuesday 11 June 2013

Preprocessor Operators/Directives/Macros/Pragmas in c

Here are some preprocessor operators/directives/macros in  c

1) PREPROCESSOR OPERATORS

Stringizing operator (#):-Causes the corresponding actual argument to be enclosed in double quotation marks.

charizing Operator (#@):-Causes the corresponding argument to be enclosed in single quotation marks and to be treated as a character (Microsoft Specific).

Token Pasting Operator (##):-Allows tokens used as actual arguments to be concatenated to form other tokens.

DEFINED operator:-Simplifies the writing of compound expressions in certain macro directives.



2) PREPROCESSOR DIRECTIVES

#                 (NULL DIRECTIVE)
----------------------------------------------------------------------------------------------------------------------------------
#include

#if
#else
#define

Example:-

#include<stdio.h>
#include<conio.h>
#define DLEVEL 1

#if DLEVEL > 5
    #define SIGNAL  1 //this is disable
   
#else
    #define SIGNAL  0  //////////this is enable
  
#endif

void main ()
{

printf("Hi\n");
_getch();
}

------------------------------------------------------------------------------------------------------------
#endif
#elif

Example:-

#include<stdio.h>
#include<conio.h>
#define DLEVEL 1

#if DLEVEL == 0
    #define STACK 0
#elif DLEVEL == 1
    #define STACK 100 //////////only this is enable ,rest of all are disable
#elif DLEVEL > 5
    #define STACK 300
#else
    #define STACK 200
#endif

void main ()
{

printf("Hi\n");
_getch();
}

------------------------------------------------------------------------------------------------------------

#ifdef
#ifndef
#undef

Example:-

#include<stdio.h>
#include<conio.h>
#define DLEVEL 1
#undef DLEVEL ////undefine the DLEVEL


#ifdef DLEVEL
#define SIGNAL //////this is disable
#endif
#ifndef DLEVEL
#define final     //////////this is enable
#endif
void main ()
{

printf("Hi\n");
_getch();
}


-----------------------------------------------------------------------------------------------------------
#line:-The #line directive tells the preprocessor to change the compiler's internally stored line number and filename to a given line number and filename.

#line digit-sequence ["filename"]
#using:-Imports metadata into a program compiled with /clr.

#using file [as_friend]

#import:-Used to incorporate information from a type library. The content of the type library is converted into C++ classes, mostly describing the COM interfaces.

#error:-Error directives produce compiler-time error messages.

Example:-


#include<stdio.h>
#include<conio.h>
#define DLEVEL 1

#undef DLEVEL

#ifndef DLEVEL
#error DLEVEL is not defined
#endif

void main ()
{

printf("Hi\n");
_getch();
}

When we compile this program apart from an intellisense error it displays a compilation error which shows following message:-

error C1189: #error :  DLEVEL is not defined

3) PREDEFINED MACROS

__DATE__:-The compilation date of the current source file. The date is a string literal of the form Mmm dd yyyy. The month name Mmm is the same as for dates generated by the library function asctime declared in TIME.H.

__FILE__:-It returns the name of the current source file.

__LINE__:-It returns the line number in the current source file .

Here is an use of __FILE__ and __LINE__ where we want to know in which file the error occurs and at which line so we use the following syntax.


#define CudaCheckError(cudaStatus) if (cudaStatus != cudaSuccess) {printf("\n%s\t%s\t%d", cudaGetErrorString(cudaGetLastError()),__FILE__,__LINE__);_getch();}

It returns the cuda error was which type ,in which file and in that file at which line.


__TIME__:-The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.

__TIMESTAMP__:-The date and time of the last modification of the current source file, expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy, where Ddd is the abbreviated day of the week and Date is an integer from 1 to 31.


_FUNCTION_:-Valid only within a function and returns the undecorated name of the enclosing function (as a string). 

_cplusplus:-Defined for C++ programs only.

_WIN32:-Defined for applications for Win32 and Win64. Always defined.

_WIN64:-Defined for applications for Win64.

_OPENMP:-Defined when compiling with /openmp, returns an integer representing the date of the OpenMP specification implemented by Visual C++.
// _OPENMP_dir.cpp
// compile with: /openmp 
#include <stdio.h> 
int main() {
   printf("%d\n", _OPENMP);
}


_DEBUG:-Defined when compiling with /LDd, /MDd, and /MTd.

_DLL:-Defined when /MD or /MDd (Multithread DLL) is specified.

_MT:-Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified.

4) PRAGMA DIRECTIVES

#pragma omp:-It takes one or more OpenMP directives, along with any optional directive clauses.

#pragma omp directive

#pragma pack:-Specifies packing alignment for structure, union, and class members.

#pragma pack( [ show ] | [ push | pop ] [, identifier ] , n  )
-----------------------------------------------------------------------------------------------------------------------------#pragma region:-lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.

#pragma endregion:-#pragma endregion marks the end of a #pragma region block.

A #region block must be terminated with #pragma endregion.


#pragma region name
#pragma endregion comment

Parameters:-
comment(optional)
A comment that will display in the code editor.
name(optional)
The name of the region. This name will display in the code editor.

------------------------------------------------------------------------------------------------------------------------------

#pragma auto_inline:-Excludes any functions defined within the range where off is specified from being considered as candidates for automatic inline expansion.

#pragma auto_inline( [{on | off}] )

The auto_inline pragma is placed before and immediately after a function defintion.It can not be declare in function definition.The pragma takes effect at the first function defintion after the pragma is seen.

#pragma once:-
Specifies that the file will be included (opened) only once by the compiler when compiling a source code file.This can reduce build times as the compiler will not open and read the file after the first #include of the module.
-- 

No comments:

Post a Comment