Table of contents
1.
Introduction
2.
Syntax
3.
How does #undef work?
4.
Example 1
4.1.
Code
4.2.
Explanation
4.3.
Output
5.
Example 2
5.1.
Code
5.2.
Explanation
5.3.
Output
6.
Frequently Asked Questions
6.1.
What are the different types of preprocessors?
6.2.
Why are preprocessor directives?
7.
Conclusion
Last Updated: Mar 27, 2024

C #undef

Author Ayushi Poddar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the C programming language, #undef is a directive that can help you remove all the definitions for the given macro name or any other constant defined using the #define directive. This is a  part of the preprocessor directive because it is called by the compiler automatically before actual compilation begins. Before the compilation process of the C program starts, the compiler source code is processed, and that's why this process is named preprocessing. All the different commands used for the preprocessor are known as the preprocessor directives, and all preprocessor directives are defined using the #.

Also see: C Static Function

Click on the following link to read further: Features of C Language

Syntax

In C, we are provided with a feature called preprocessors, which is used to process the source code written by the programmer before it is compiled. Before the program is passed through a preprocessor, specific instructions, such as directives, are looked for and understood by the preprocessor. These preprocessor directives must begin with a # sign.

The preprocessor is the part of the compiler that executes the essential operations in the given code. Transformations performed by the preprocessors are lexical, which also tells that the preprocessor's output is in the form of text.

In order to define a macro, use the following syntax:

#define macro_name

The above line is passed to the preprocessor, and this assigns a 3.14 value to the PI variable, which can be used for further uses anywhere in the program. If we need to limit the scope f this macro_name within the program itself, we can use the #under to remove the previously declared macro_name for the further assignments.

#undef macro_name

In the above case, macro_name refers to the name of that variable that was defined earlier. # specifies that it’s the preprocessor directive and must be compiled using the preprocessor.

How does #undef work?

Preprocessors aim to refer to the programs processed in our source code before the code even enters the compiler. The user writes the source code, and this source code is then sent for preprocessing to the preprocessors that generate an expanded source file, having the name same as that of the program. The expanded file is forwarded for compilation to the compiler to create an object code of the library functions.

Now when the #undef macro_name command is encountered by the preprocessor, it removes the macro from memory such that it can be used again. We may also use the #ifdef and #endIf directive, which helps us to check if any macro name exists or not.

Example 1

Code

#include <stdio.h>
#define num 10
int s1=num*num;
#undef num
#define num 11
int s2=num*num;
int main() 
{
	printf("Value of square with first value of num variable is = %d",s1);
	printf("\n");
	printf("Value of square with second value of num variable is = %d",s2);
	return 0;
}

Explanation

In the above code, we declared a num variable with a value of 10 and then undefine it by using the undef directive. We then again define the variable with value 11 and then see how the value of variables s1 and s2 change.

Output

Value of square with first value of num variable is = 100
Value of square with second value of num variable is = 121

Example 2

Code

#include <stdio.h>
#define num 7
int square1=num1*num1;
#undef num1
int main() 
{
	printf("Value of constant num that has been removed using #undef directive  = %d",num1);
	return 0;
}

Explanation

In the above code, we see what happens when we try to access a constant or a macro that is defined using #define but later has been removed using #undef directive.

Output

Main.c.3:13: error: ‘num1’ undeclared here (not in a function)
Int square1=num1*num1;

You can also read about dynamic array in c, Short int in C Programming and Tribonacci Series

Frequently Asked Questions

What are the different types of preprocessors?

There are 4 categories of preprocessors which includes macros, file inclusion, conditional compilation, and other directives.

Why are preprocessor directives?

All the commands used for the preprocessor are known as preprocessor directives and all preprocessor directives are defined using #.

Recommended Topic -  singleton design pattern in c#

Conclusion

This article extensively discussed C #undef. We hope that this blog has helped you enhance your knowledge regarding C #undef and if you would like to learn more, check out our articles here.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blog to help other ninjas grow. Happy Coding!

 

Live masterclass