PSYCHOCODES

PsychoCodes.in

Macros in C with examples C Tutorials

Macros are C statements which are defined as a pre-processor directive, C macros are defined using the #define pre-processor keyword, pre-processor statements are processed by the compiler before entering into the main function of the program.

Also Read: C program to implement Multithreading-Multithreading in C

In this tutorial, we are going to learn how to define macros and use them in our C programs, Macros are mainly of two types:

  • Predefined Macros
  • User-defined Macros

Predefined Macros is C

C has a number of predefined macros some of them are listed below:

  • __DATE__: Prints the current date as a character literal in "MMM DD YYYY" format.
  • __TIME__: Prints the current time as a character literal in "HH:MM:SS" format.
  • __FILE__: Prints the absolute path of the working C file.

C program to demonstrate predefined macros

#include<stdio.h>

int main(){
	printf("Path of the file: %s\n",__FILE__);
	printf("Todays Date: %s\n",__DATE__);
	printf("Time: %s\n",__TIME__);
	
	return 0;
}

Output:

Path of the file: C:\Users\username\Documents\premacro.c
Todays Date: Jan 30 2018
Time: 20:25:45

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

User-defined Macros in C

In this section, we will learn how to define your own C macros and use them in your C programs. First, let us see a simple example based on C functions.

#include<stdio.h>

void printName(){
	printf("My name is Lucifer.");
}

int main(){
	printName();
	return 0;
}

Above function will simply print the given string on the console, now we will write a simple macro for the same function in C.

#include<stdio.h>
#define myNewMacro() (printf("This is a simple Macro"))

int main(){
	myNewMacro();
	return 0;
}

Output:

This is a simple Macro
--------------------------------

Important Note: Make sure in the above you do not enter any line breaks while defining the macro, we will also see how to define multiline macros later in this article.

Passing arguments to a Macro in C

You can pass arguments to a macro just like you pass them in a function, see the below example.

#include<stdio.h>
#define sum(a,b) (printf("sum is : %d",(a+b)))

int main(){
	sum(23,45);
	return 0;
}

Output:

sum is : 68
--------------------------------

You can also return a value using macros let us see a simple C function for comparing two numbers which will return the greater one.

#include<stdio.h>

int greater(int a,int b){
	return (a>b)?a:b;
}

int main(){
	printf("Greater Number is : %d",greater(45,124));
}

Now we will write a macro for the above-defined function.

#include<stdio.h>
#define greater(a,b) ((a>b)?a:b)

int main(){
	printf("Greater Number is : %d",greater(45,124));
}

Multiline Macros in C

In this section, we will learn how to define multiline macros in C to define multiline macros we are going to use the (\) macro continuation operator. See the below example.

#include<stdio.h>
#define multiline() \
	printf("hello\n"); \
	printf("what's up");

int main(){
	multiline()
}

In the above macro, we have used the continuation operator(\) whenever we jumped to a new line.

hello
what's up
--------------------------------

If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts