Skip to main content

Preprocessors and Macros in C

 

Preprocessors and Macros in C

Pre-processors in C are micro-processor that is basically used by compilers. It converts the code and replaces it at the line where the pre-processor used in your code before the actual compilation of your program. This is the reason the process is known as pre-processing.

It is said to be a micro-pre-processor as it allows you to ass macros in the program.

All the commands used in Pre-processor are called the pre-processor directives. Preprocessor directives should always begin with the “#” symbol and should begin in the first column.

Types of Pre-Processors Used in C

Preprocessor

Syntax/Description 

Macro

Syntax: #define
This macro defines constant value and can be any of the basic data types.

Header file inclusion

Syntax: #include <file_name>
The source code of the file “file_name” is included in the main program at the specified place.

Conditional compilation

Syntax: #ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in source program before compilation with respect to the condition. 

Other directives

Syntax: #undef, #pragma
#undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after the main function in a C program.

 

List of Few Pre-Processor Directives and Its Short Description

Sr.No.

Directive & Description

1

#define

Substitutes a preprocessor macro.

2

#include

Inserts a particular the header from another file.

3

#undef

Undefines a preprocessor macro.

4

#ifdef

Returns true if this macro is defined.

5

#ifndef

Returns true if this macro is not defined.

6

#if

Tests if a compile time condition is true.

7

#else

The alternative for #if.

8

#elif

#else and #if in one statement.

9

#endif

Ends preprocessor conditional.

10

#error

Prints error message on stderr.

 

Macros in C:

Macros are generally used to define constant values that are being used repeatedly in programs.

Macros can even accept arguments and such macros are known as function-like macros.

It can be useful if tokens are concatenated into code to simplify some complex declarations.

Macros provide text replacement functionality at pre-processing time.

A macro is a name given to a block of C statements as a pre-processor directive.

Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function).

A macro is defined with the preprocessor directive, #define

Types of Macros in C

A macro is a segment of code that is replaced by the value of the macro. Macro is defined by #define directive. There are two types of macros:

1.     Object-like Macros

2.     Function-like Macros

 

Object-Like Macro:

The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants. For example:

#define PI 3.14 

Here, PI is the macro name that will be replaced by the value 3.14.

#Define Macro:

In the C Programming Language, the #define directive allows the definition of macros within your source code.

These macro definitions allow constant values to be declared for use throughout your code.

Macro definitions are not variables and cannot be changed by your program code like variables.

You generally use this syntax when creating constants that represent numbers, strings, or expressions.

Constant

#define CNAME value

OR

#define CNAME (expression)

CNAME

The name of the constant. Most C programmers define their constant names in uppercase, but it is not a requirement of the C Language.

value

The value of the constant.

expression

An expression whose value is assigned to the constant. The expression must be enclosed in parentheses if it contains operators.

Note: Do NOT put a semicolon character at the end of #define statements. This is a common mistake.

Number

#define directive to define a numeric constant:

Example: The constant named AGE would contain the value of 10.

#define AGE 10

String

#define directive to define a string constant.

Example: The constant called NAME would contain the value of "C Programming".

#define NAME "C Programming"

Expression

Example: The #define directive to define a constant using an expression.

#define GRADE (20 / 2)

Example 1:



Example 2:


Example 3:


Output:

Example 4:



Output:

 

Example 5:

 


Output:

 



Function Like Macro:

The function-like macro looks like the function call. For example:

#define MIN(a,b) ((a)<(b)?(a):(b))   

Here, MIN is the macro name.

Example :


Output:

 


Predefined Macros in C:

Sr.No.

Macro & Description

1

__DATE__

The current date as a character literal in "MMM DD YYYY" format.

2

__TIME__

The current time as a a character literal in "HH:MM: SS" format.

3

__FILE__

This contains the current filename as a string literal.

4

__LINE__

This contains the current line number as a decimal constant.

5

__STDC__

Defined as 1 when the compiler complies with the ANSI standard.

 

 

Dr. Arpana Chaturvedi

Asst Prof (JIMS-BCA)

Department of Information Technology and Software Development

Comments

Post a Comment

Popular posts from this blog

Keyboard Shortcuts used in Most Popular Jupyter Notebook

Jupyter Notebook is a widely used notebook App by Data Scientists because of its rich features. The app allows us to write Python Code as well as rich text elements like formatted paragraphs, Images, links, mathematical equations, etc. It is a client server-based application that allows programmers, analysts for editing notebooks, write and execute python codes by browsers. JIMS, BCA course includes Python in the first year and AI using Python in the final year. Students are asked to work on Jupyter Notebook. As a student and to become a skilled developer, one must know the shortcuts to be used in any application. In this article, students can find the list of shortcuts they can use while practicing in Python and later on while learning AI using Python . JIMS, Vasant Kunj-II , is one of the best BCA colleges in Delhi NCR , which provides an  updated curriculum . The syllabus includes almost all the latest technologies that are in demand including Analytics using Excel, ...

Machine Learning and Role of Model in Machine Learning

Machine Learning concept is used in developing different modeling technique that involves Data means information such as documents, images, etc. The modeling technique analyzes the data and finds the suitable model by itself rather than having human interference in it. Data used by the Machine Learning modeling process are of two types, one is called “Training” data and the second one is “Test” Data. The primary idea of Machine Learning is to achieve a model using the training data. Types of Machine Learning Techniques: There are three types of Machine Learning Modelling Techniques. These are: Supervised Learning : In this Modelling Technique each training dataset should consist of input and correct output pairs. Unsupervised learning: This modeling technique is used for investigating the characteristics of the data and for preprocessing the data Reinforcement learning: This technique employs sets of input, some output, and grade as training data. Fig. 1. Types of Machi...
  Flutter – The Future of Mobile App Developer Flutter is a very popular Googles SDK used popularly for Mobile App Development. The Mobile App Developers feels easy to use Flutter because of its features like cross-platform Application, customized plenty of Easy-to-use Widgets and is a single codebase for any platform. Introduction: Flutter is a Cross-Platform App Development Framework enables the programmer to build an application that can run on Android as well as iOS. It uses a common codebase to design an application as it has an engine designed in the most popular language, C++, commonly used by everyone and easy to understand. Flutter is a Google product and the alpha version of Flutter was released at Google I/O/2017 and since then it has been improved drastically by all of its leaps and bounds. Google developed to assist ambient computing and becomes so popular because of its powerful and easy-to-use features. It is used by many Companies like Grab, Groupon, eBay, ...