×

#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

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

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.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.