In C programming, #define is a preprocessor directive that is used to define macros. The macros are the identifiers defined by #define which are replaced by their value before compilation. We can define constants and functions like macros using #define. The generics in C are also implemented using the #define preprocessor directive along with _Generic.
Syntax of C #define
The syntax of #define preprocessor directive in C is:
For Defining Constants
#define MACRO_NAME value
For Defining Expressions
#define MACRO_NAME (expression within brackets)
For Defining Expression with Parameters
Arguments passed in the macros can be used in the expression.
#define MACRO_NAME(ARG1, ARG2,..) (expression within brackets)
There are a few more ways using which we can define macros. To know more, refer to this article - Macros and its types in C
Examples of C #define
Example 1:
In the below example, we have defined a macro 'PI' and assigned it a constant value which we can use later in the program to calculate the area of a circle.
C
// C Program to illustrate how to use #define to declare
// constants
#include <stdio.h>
// Defining macros with constant value
#define PI 3.14159265359
int main()
{
int radius = 21;
int area;
// Using macros to calculate area of circle
area = PI * radius * radius;
printf("Area of Circle of radius %d: %d", radius, area);
return 0;
}
OutputArea of Circle of radius 21: 1385
Example 2:
In the below example, we have defined a macro 'PI' and assigned it an expression, and that value of the expression is used in the program using 'PI'.
C
// C Program to illustrate the defining of expression using
// #define
#include <stdio.h>
// Defining macros with expression
#define PI (22 / 7)
int main()
{
int radius = 7;
int area;
// Using macros to calculate area of circle
area = PI * radius * radius;
printf("Area of Circle of radius %d: %d", radius, area);
return 0;
}
OutputArea of Circle of radius 7: 147
Example 3:
In the below example, we have defined two macros CIRCLE_AREA and SQUARE_AREA with a parameter and that parameter is used in the expression to calculate the area of circle and square respectively.
C
// C Program to define the function like macros using
// #define
#include <stdio.h>
// Defining parameterized macros with expression
#define CIRCLE_AREA(r) (3.14 * r * r)
#define SQUARE_AREA(s) (s * s)
int main()
{
int radius = 21;
int side = 5;
int area;
// Using macros to calculate areas by
// passing argument
area = CIRCLE_AREA(radius);
printf("Area of Circle of radius %d: %d \n", radius,
area);
area = SQUARE_AREA(side);
printf("Area of square of side %d: %d", side, area);
return 0;
}
OutputArea of Circle of radius 21: 1384
Area of square of side 5: 25
Important Points
- Macros declared using #define are used to store constants and cannot be changed. we cannot assign variables to the macros.
- We cannot use the '=' operator to assign value to the macros (eg. #define PI 3.14).
- We do not use the semicolon ';' at the end of the statement in #define.
Similar Reads
#define in C++ In C++, #define is a preprocessor directive used to define a macro. Macros are a way to represent a fragment of code or a constant value by giving it a name. When the preprocessor encounters the macro name in the code, it replaces it with the corresponding code fragment or value that is defined usin
4 min read
fwrite() in C In C, fwrite() is a built-in function used to write a block of data from the program into a file. It can write arrays, structs, or other data to files but is especially designed to write binary data in binary files.Example:C#include <stdio.h> int main() { FILE *fptr = fopen("gfg.bin", "wb"); i
3 min read
getx() function in C The header file graphics.h contains getx() function which returns the X coordinate of the current position. Syntax : int getx(); Example : Explanation : Initially, the X coordinate of the current position is 0. On moving the coordinates using moveto() function, the X coordinate changes to 80. Below
2 min read
gety() function in C The header file graphics.h contains gety() function which returns the Y coordinate of the current position. Syntax : int gety(); Example : Explanation : Initially, the Y coordinate of the current position is 0. On moving the coordinates using moveto() function, the Y coordinate changes to 50. Below
2 min read
sprintf() in C Syntax: int sprintf(char *str, const char *string,...); Return: If successful,it returns the total number of characters written excluding null-character appended in the string, in case of failure a negative number is returned .sprintf stands for âString printâ. Instead of printing on console, it sto
1 min read
Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
C Identifiers In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or any other user-defined items. It is a name that uniquely identifies a program element and can be used to refer to it later in the program.Example:C// Creating a variable int val = 10; // Creatin
3 min read
C++ printf() Function printf() function is originally declared under the <cstdio>header file. It prints the formatted string to the standard output stdout. Syntax: int printf(const char*word, .......) Parameters: word: represents the string that needs to be printed on the standard output stdout,....... : represents
3 min read
scanf in C In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.Example:C#include <stdio.h> int main() { int n; // Reading an integer input scanf("%d",
3 min read
Tokens in C In C programming, tokens are the smallest units in a program that have meaningful representations. Tokens are the building blocks of a C program, and they are recognized by the C compiler to form valid expressions and statements. Tokens can be classified into various categories, each with specific r
4 min read