×

#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

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 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.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.