×

#undef in C

#undef in C

In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef preprocessor directive will inform the compiler to remove the definitions which have been specified for the macro. The macro can be redefined once it has been removed using the #undef directive. Once the macro has been undefined using the #undef directive, then the directive #ifdef will be evaluated to false on that macro.

In other words, the #undef directive can be defined as a directive which removes all the declarations for that particular macro name or any constant which is defined using the #define directive. Before the compiler compiles the C program, the main code will be processed; thus, the process is called preprocessing.

All the commands used for this preprocessor directive are defined using the “#” symbol. Once the constant is declared using the directive #define, it will be limited to its cope using #undef directive in an extensive program.

Any program or application consisting of many macros becomes hard to understand. It becomes even more complicated when the set of macros that are defined are not stable. Hence, during such times, the #undef directive will decrease the readability of the macros.

Therefore, well defined use of #undef directive improves readability and is more economical. This rule raises an issue when a #undef undefines a macro that was defined in another file. It will also raise an issue for an #undef directive that tries to undefine a non-existing macro.

Syntax:

The syntax for the #undef directive is:

             #undef macro
                         Or
             #undef token 

Where,

macro: is the name of the macro which the preprocessor will remove.

E.g.:

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

Output:

 In function 'main':
 /tmp/1gtohD0Zxe.c:5:16: error: 'PI' undeclared (first use in this function) 

The #undef directive is generally used to declare the preprocessor constant or a macro until a limited scope so that the developer can declare the constant once more.

When the #define preprocessor directive is passed with the value of PI being 3.14, the value of that variable can be further used anywhere in the application or the program by the developer.

Further, if one needs to limit the macro scope within the program, they have to undefine that macro using the #undef directive to remove the macro for further purposes.

Again once we undefine the macro, using that name of the variable used earlier, such as we have undefined the macro P I, which has the value 3.14, it will be removed.

 #include <stdio.h> 
 #include <conio.h>
 #define number 15 
 int square=number*number; 
 #undef number 
 int main()
 { 
 printf("The square of the number 15 is: %d \n",square); 
 }  

Output:

The square of the number 15 is: 225

 #include <stdio.h>
  #include <conio.h>
 #define YEARS_OLD 12
 #undef YEARS_OLD
 int main()
 {
 #ifdef YEARS_OLD
 printf("Tutorials and examples is over %d years old.\n", YEARS_OLD);
 #endif
 printf("Tutorials and examples is a great resource.\n");
 return 0;
 } 

Output:

Tutorials and examples is a great resource.

In this example, the YEARS_OLD macro is first defined with a value of 12 and then undefined using the #undef directive. Since the macro no longer exists, the statement #ifdef YEARS_OLD evaluates to false. This causes the subsequent printf function to be skipped.

  • The developers’ source code will be sent to the preprocessor for the preprocessing, which later gives an expanded code file with the same name as that of the program or an application. That file will be further sent to the compiler for future compilation and generated an object code of the library functions, and the executable file will be generated.
  • The #undef directive command found by the preprocessor recognizes the # symbol, and the preprocessor will check the macro with the same name. Once it matches, that following macro will be eliminated from the memory so that it cannot be used again. If the macro which is already being used is assigned, it throws an error.
  • The user can use the #ifdef…#endif directive to check if the macro they want to use exists or has been deleted.

Related Topics

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

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

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.