Preprocessor - Directives - For C - Compiler - 29-May-2024 - SRC
Preprocessor - Directives - For C - Compiler - 29-May-2024 - SRC
#include<stdio.h>
int main()
{
int number=50;
printf("value of number is %d, address of number is %u",number,&number);
return 0;
)
Preprocessor Directives in C
● Definition:
● Preprocessor directives are instructions processed by the
Preprocessor like #define #ifndef Directives in C [email protected] 29-May-2024 Page 1 of 8
Preprocessor like #define #ifndef Directives in C [email protected] 29-May-2024 Page 2 of 8
#define:
● Purpose: #defines macros(preferably in CAPITAL letters) for
constants or expressions that are replaced throughout the code.
● Syntax: #define identifier value
Example:
#define PI 3.14
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#include:
Purpose: Includes the contents of another file into the source file.
Syntax:
Example:
#include <stdio.h>
#include "myheader.h"
Usage: Facilitates code reuse and modularity by allowing the inclusion of library
functions and user-defined headers.
#ifdef DEBUG
printf("Debug mode\n");
#endif
#undef:
Preprocessor like #define #ifndef Directives in C [email protected] 29-May-2024 Page 5 of 8
Preprocessor like #define #ifndef Directives in C [email protected] 29-May-2024 Page 6 of 8
int main() {
#ifdef TEMP
printf("TEMP is defined.\n");
#else
printf("TEMP is not defined.\n");
#endif
return 0;
}
#pragma:
The #pragma directive in C is used to provide special instructions to the compiler. These
instructions can control specific compiler behaviors, enable or disable certain features, and
help optimize the code. The usage of #pragma varies across different compilers, and each
compiler may support different #pragma commands. Below are some common uses of the
#pragma directive with examples.
// myheader.h
#pragma once
void myFunction();
Summary: