Preprocessor and Macros

All preprocessing directives begin with a #
symbol. For example #define PI 3.14

Including Header Files

#include <stdio.h>
#include "my_header.h"

Macros using #define

#define c 299792458  // speed of light
#define circleArea(r) (3.1415*(r)*(r))

Example

#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)

int main() {
    float radius, area;

    printf("Enter the radius: ");
    scanf("%f", &radius);
    area = circleArea(radius);
    printf("Area = %.2f", area);

    return 0;
}

Predefined Macros

__DATE__
A string containing the current date.

__FILE__
A string containing the file name.

__LINE__
An integer representing the current line number.

__STDC__
If follows ANSI standard C, then the value is a nonzero integer.

__TIME__
A string containing the current time.

__BASE_FILE__ This macro expands to the name of the main input file, in the form of a C string constant. This is the source file that was specified on the command line of the preprocessor or C compiler.

__FILE_NAME__ This macro expands to the basename of the current input file, in the form of a C string constant. This is the last path component by which the preprocessor opened the file. For example, processing “/usr/local/include/myheader.h” would set this macro to “myheader.h”.

Example

#include <stdio.h>
int main()
{
   printf("Current time: %s",__TIME__);   
}