×

#define in C

#define in C

In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the micro substitution.

The #define directive can use any of the basic data types present in the C standard. The #define preprocessor directive lets a programmer or a developer define the macros within the source code.

This macro definition will allow the constant value that should be declared for the usage. Macro definitions cannot be changed within the program’s code as one does with other variables, as macros are not variables.

The #define is usually used in syntax that created a constant that is used to represent numbers, strings, or other expressions.

The #define directive should not be enclosed with the semicolon(;). It is a common mistake done, and one should always treat this directive as any other header file. Enclosing it with a semicolon will generate an error.

The #define creates a macro, which is in association with an identifier or is parameterized identifier along with a token string. After the macro is defined, then the compiler can substitute the token string for each occurrence of the identifier within the source file.

Syntax:

 #define NAME value /* this syntax creates a constant using define*/
             Or
 #define NAME (expression) /* this syntax creates a constant using define*/
 Where; 

NAME: is the name of a particular constant. It can either be defined in smaller case or upper case or both. Most of the developers prefer the constant names to be in the upper case to find the differences.

value: defines the value of the constant.

Expression: is the value that is assigned to that constant which is defined. The expression should always be enclosed within the brackets if it has any operators.

E.g.:

 #include <stdio.h> 
 #include <conio.h>
 #define PI 3.14 
 int main()
 { 
 printf("%f",PI); 
 }  

Output:

3.140000
 #include <stdio.h> 
 #define MIN(a,b) ((a)<(b)?(a):(b)) 
 void main() { 
 printf("The minimum value between 50 and 20 is: %d\n", MIN(50,20));   
 }  

Output:

The minimum value between 50 and 20 is: 20

Number

The following is an example of how you use the #define directive to define a numeric constant:

E.g.:

#define AGE 10

In this example, the constant named AGE would contain the value of 10.

String

You can use the #define directive to define a string constant.

E.g:

 #define NAME "TuotiralsAndExamples.com"
 In this example, the constant called NAME would contain the value of "TutorialsAndExamples.com".
 #include <stdio.h>
 #include <conio.h>
 #define NAME "TuotiralsAndExamples.com"
 #define AGE 10
 int main()
 {
 printf("%s is over %d years old.\n", NAME, AGE);
 return 0;
 } 

Output:

TuotiralsAndExamples.com is over 10 years old.

Expression

You can use the #define directive to define a constant using an expression.

E.g.:

 #define AGE (20 / 2)
 In this example, the constant named AGE would also contain the value of 10.
 #include <stdio.h>
 #include <conio.h>
 #define AGE (20 / 2)
 int main()
 {
 printf("TuotiralsAndExamples is over %d years old.\n", AGE);
 return 0;
 } 

Output:

TuotiralsAndExamples.com is over 10 years old.

The #define is a preprocessor directive allows you to specify a name and replacement text. As the preprocessor parses the source file, each occurrence of the name is replaced by its associated text. The scope of #define is limited to the file in which it is defined. So, #defines which are created in one source file are NOT available in a different source file.

Typically, #defines is shared between multiple files are stored in a header file (*.h) which is included in each source file that requires the #define.


Related Topics

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.