echo '' ;
C Programming Language

C – Preprocessor and Macros

Before learning the C macro, you should know about C preprocessor directives in C language. In this post, you can clearly understand the following

  • what preprocess commands are available in C?
  • How do you declare the preprocessor command.?
  • what is a macro?
  • What is the macro syntax?
  • how to declare the macro?
  • how to assign the macro value?
  • how can use the macro function in c.

Here your answer, if you have any doubts please post in doubts about C Preprocessor and Macros.

In C language, totally 11 pre-processor commands are there,

Preprocessor CommandDescription
#defineSubstitutes a preprocessor macro.
#includeInserts a particular header from another file.
#undefUndefines a preprocessor macro.
#ifdefReturns true if this macro is defined.
#ifndefReturns true if this macro is not defined.
#ifTests if a compile time condition is true.
#elseThe alternative for #if.
#elifelse and #if in one statement.
#endifEnds preprocessor conditional.
#errorPrints error message on stderr.
#pragmaIssues special commands to the compiler, using a standardized method.

The below table is the syntax, rule and compiling stage of preprocessor commands

Syntax#include: For header file #include <stdio.h>

#define: For macro #include MACRO_NAME or #include MACRO_NAME MACRO_VALUE
RuleAll the preprocessor command should start with "#" (hash) and followed by an input file or macro.

Preprocessor declaration should be a non-blank character, If you do not mention the # for include files or macro then the compiler will give an error.
Preprocessor command in Compiling stage#include: While compiling stage, it will remove the #Include and replace the header file PATH in the source code.

#define: While compiling stage, also remove  #define (Macro)  with replacing macro name with code

What is MACRO in C?

Macros are the names of text/ literal values/ string (constant values) or code fragments, which will expand when the pre-processor processes the macro. The pre-processor processes the macros at compile-time; hence that macros replace with the corresponding code fragments.

A macro can execute faster than a function: As the code of macro gets expanded at the line of call, therefore macro gets executed faster with no overhead of context switch.

Syntax

  • #define <MACRO_NAME> <MACRO_VALUE>
  • Here #define denote user declaring a macro in c
  • <MACRO_NAME> user can provide any char, String
  • <MACRO_VALUE> user can provide any value to assign the value

Rules

  • You should give the space in between #define and Macro Name.
  • You should give the space in between Macro Name and Macro Value.
  • No need to add the ; semicolon in end of the declaration as like variable value assignment.

Declaration

You should declare the macro with proper syntax like #define MACRO_NAME

Assign Macro Value

you can assign any values, Stings and athematic actions in macro

  • Assign macro as string value: #define WEBSITE "www.ArunEworld.com" Here WEBSITE is the macro and www.arunEworld.com is the macro string value.
  • Assign macro as char value: #define LETTER 'A' Here LETTER is the macro and 'A' is the macro char value.
  • Assign macro as digits value: #define DIGIT 2, Here DIGIT is the macro and 2 is the macro digit value.

C Preprocessor and Macros Examples

Here You can find many more examples related to C Preprocessor and Macros


Understand of Macro while compilation time

#define MAX_NUM 15  

Referring to the sample above, what is MAX_NUM?

(A) MAX_NUM is a precompiler constant.

(B) MAX_NUM is a preprocessor macro.

(C) MAX_NUM is an integer variable.

(D) MAX_NUM is a linker constant.

Answer : MAX_NUM is a preprocessor macro.


How to print the Macro value in C

While assign the macro value, user should know the value type in order to print the macro using printf() function

  • If Macro is like #define WEBSITE "ArunEworld.com" then you can print the macro using printf("%s", WEBSITE). If you try to print as a printf("%d", WEBSITE) then compiler give the warning [warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]] but it will print the corresponding putput value 4195812 This 4195812 is belong to address of the "www.ArunEworld.com" string storing address in memory.
  • If Macro is like #define VALUE 30 then you can print the macro using printf("%d", VALUE). If you try to print as a printf("%c", VALUE) then corresponding ASCII value will be print.

Macro basic example-1

In this below code you can clearly understood what happening when we use the macro

Example-1: Actual Code:

#include <stdio.h>

#define OPEN_CURLY_BRACE    {
#define CLOSE_CURLY_BRACE   }
    
int main()
OPEN_CURLY_BRACE
    printf("ArunEworld");
    return 0;
CLOSE_CURLY_BRACE

Example-1:While Compiling the above code(Actual Code)

#include <stdio.h>

#define OPEN_CURLY_BRACE    {
#define CLOSE_CURLY_BRACE   }

int main()
{
    printf("ArunEworld");
    return 0;
}

Example-2:Actual Code

#include <stdio.h>

#define INT_TYPE int
INT_TYPE main()
{
    printf("ArunEworld");
    return 0;
}

Example-2: While compiling above code(Actual Code)

#include <stdio.h>

int main()
{
    printf("ArunEworld");
    return 0;
}


Two time declare the same macro but different value assignment

If your declared the same macro two times but different value then warning [warning: “MACRO_NAME” redefined] should be generated by compiler and print the last declared macro value in console

Code

#include <stdio.h>

#define MACRO_NAME "ArunEworld"
#define MACRO_NAME "ArunEworld Macro re-defined example"

int main()
{
    printf("%s", MACRO_NAME);

    return 0;
}

Output:

main.c:12:0: warning: "MACRO_NAME" redefined
main.c:11:0: note: this is the location of the previous definition
ArunEworld Macro re-defined example   

How you define Macro for how many seconds in Year?

  • #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL

How you define Marco using find small number?

  • Answer : #define MIN(x,y) ((x<y)?x:y)

Example

#include <stdio.h>
#define MIN(x,y) ((x<y)?x:y)

int main()
{
    int a,b,min;
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b); 
    
    min=MIN(a,b);
    printf("Minimum number is: %d\n",min);
    
    return 0;
}
/*
Enter first number: 100 
Enter second number: 200
Minimum number is: 100
*/

Find out MIN & MAX using MACRO in C?

  • Answer : #define MIN(X,Y) (X<Y ? X:Y)  or #define MAX(X,Y) (X>Y ? X:Y)

Example code:

#include <stdio.h>
#define MIN(x,y) ((x<y)?x:y)

int main()
{
    int a,b,min;
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b); 
    
    min=MIN(a,b);
    printf("Minimum number is: %d\n",min);
    
    return 0;
}

Output:

/*
Enter first number: 100 
Enter second number: 200
Minimum number is: 100
*/

Write a macro to find the smallest number among 3 given numbers?

Answer :

Example Code :

#include<stdio.h>
#define MIN(X,Y)(X<Y ? X:Y)
#define SMALL(X,Y,Z)(MIN(X,Y)<Z ? MIN(X,Y):Z)
void main()
{
    int FIRST,SECOND,THIRD;
    clrscr();
    printf("ENTER NUMBERS TO COMPARE\n");
    printf("\nFIRST NUMBER : ");
    scanf("%d", &FIRST);
    printf("\nSECOND NUMBER : ");
    scanf("%d", &SECOND);
    printf("\nTHIRD NUMBER : ");
    scanf("%d", &THIRD);
    printf("\nThe SMALLER NUMBER IS : %d", SMALL(FIRST,SECOND,THIRD));
    getch();
}

Output

ENTER NUMBERS TO COMPARE 
FIRST NUMBER : 11
SECOND NUMBER : 22
THIRD NUMBER : 33
The SMALLER NUMBER IS : 11

Swap Two Numbers using MACRO

  • #define SWAP(x, y) (x ^= y ^= x ^= y)

MACRO Example of complete code

#include<stdio.h>
#define S printf("ArunEworld");
#define AEW(S) int main()\
             {\
                S\
                return 0;\
             }
AEW(S)

/* Output: ArunEworld */

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading