Skip to main content

Macros Vs Functions

Macros Vs Functions


Macros


It contains few lines of code
A macro is preprocessed
A macro is expanded in the  code
The program size increases
Execution of macros is  faster since code is replaced
No type checking is done for macros
Usually used for small code  which appear many times

Functions

It contains several lines of code
A function is compiled
Function code is not inserted in  the code
The program size does not  increase
Functions execute slower
Strict type checking is done for  functions.
Usually used for large code that  needs to be executed many times.

Conditional Compilation Directives


These directives allow specific parts of the program to be included or  discarded based on certain condition. It causes the preprocessor to  conditionally suppress the compilation of portions of source code. 

#if


Syntax: #if constant-expression 
It checks whether the constant expression is true(non-zero). 

#ifdef 


Syntax: #ifdef identifier 
It checks whether the identifier is currently defined using #define  directive. 

 #ifndef 

 Syntax: #ifndef identifier 
 It checks if the identifier is not currently defined.

#else 

Syntax: #else 
It is used with #if, #ifdef, #ifndef to delimit alternative source text  to be compiled if the condition is false. It is optional. 

#elif  

Syntax: #elif constant-expression 
It is used with #if, #ifdef, #ifndef or another #elif to indicate  alternative code if the condition is false. It is optional. It must be  followed by a condition.

#endif 

Syntax : #endif newline 
This directive ends the scope of the #if, #ifdef, #ifndef, #else, #elif  directive.

Example
: Check if macro NUM has an even value & display appropriate message.

#if (NUM%2==0) 
printf( “NUM is even”); 
#endif 

Check if macro NUM has an even or odd value & display appropriate  message.

#if (NUM%2==0) 
printf( “NUM is even”); 
#else 
printf( “NUM is odd”); 
#endif

Check if macro NUM has 0, positive or negative value and display  appropriate message.

#if (NUM==0) 
printf( “\n Number is zero”); 
#elif(NUM > 0) 
printf( “\n Number is positive”); 
#else  
printf( “\n Number is negative”); 
#endif 

If macro NUM is defined, then define macro MAX having value 20 
#ifdef NUM 
#define MAX 20 
#endif

If macro NUM is not defined, then define macro NUM having value 20 
#ifndef NUM 
#define NUM 20 
#endif 

If macro NUM is defined, then redefine it with value 75. If it is not  defined, define it with value 100. 
#ifdef NUM 
#undef NUM 
#define NUM 75 
#else 
#define NUM 100 
#endif

Predifined macros


LINE


Integer value representing the current line in the source  code file

FILE


A string literal containing a C string constant which is  the name of the source file being compiled


DATE


A string literal in the form “mmm dd yyyy” containing  the date on which the preprocessor is being run

TIME


A string literal in the form “hh:mm:ss” containing the  time at which the compilation process began

Comments

Trending⚡

Happy birthday Hardik Pandya | In C programming

  Happy birthday Hardik Pandya . Now you are  28 years old. Great achievement you have. Let's we want to talk more about Hardik pandya. He is great cricketer. Pandya is awesome. In this Blog Post we are going to wish pandya " Happy birthday using C program". Let's tune with us till end. Now we have to wish pandya, so we are going to use printf () function printing message to pandya as " Happy birthday Hardik pandya Now you are 28 years old". Hardik pandya was born on 11 October in 1993. Now we are going to declare a variable called as current_age = 2021 - 1993. It calculate current age Of Hardik pandya. See the "Happy birthday pandya" using c programming. If you liked this Blog Post then don't forget to share with your computer science learning friends. Once again " Happy birthday Hardik Pandya sir". Read also Happy Rakshabandhan wish using C program Friendship day 2021 greetings in C

Understanding link.click() in JavaScript

Hey! Today i am going to share with you how to use link.click() function in javascript As a JavaScript developer, you may come across the need to programmatically click a link on a web page. The link.click() method is a commonly used way to do this, and it is important to understand how it works and when to use it. What is link.click()? link.click() is a method that can be used to simulate a click on a link element in JavaScript. It is typically used when you want to trigger a link click event programmatically, rather than requiring the user to physically click the link. How to use link.click() Using link.click() is relatively straightforward. First, you need to select the link element you want to click using a DOM selector such as getElementById() or querySelector(). Then, you can call the click() method on the link element to simulate a click. Here is an example: // select the link element const myLink = document.getElementById('my-link'); // simulate a cl

Best course Recommendation of IT field in 2023

Recommend me best course 🎉50% Discount on registration today itself 🎉! Join Course Now In today's fast-paced digital age, staying updated with the latest technology trends is crucial for professionals looking to advance in their careers. With technology rapidly evolving, it is important to continuously upskill and gain new knowledge to stay relevant in the job market. In 2023, there are several courses in technology that are gaining popularity and are highly recommended for professionals seeking to enhance their skills in data science, analytics, and digital marketing. Data Science: Data science is an interdisciplinary field that combines statistical analysis, machine learning, and computer science to extract insights and knowledge from data. With the increasing amount of data available, data scientists are in high demand across various industries. The best courses in data science for 2023 are th

What is programming explained in simple words

Hi my dear friends today in this blog post we are going to know what programming is? In simple words I will explain to you programming. Nowadays we are watching real life use of programming. How computers learn to speak, talk and do the specified complex task for us. We are all keen to know what is exactly programming? Programming is the process of creating instructions that a computer can understand and execute. These instructions, also known as code, are written in a programming language that is specific to the task at hand. The history of programming can be traced back to the mid-20th century, with the development of the first electronic computers. The first programming languages were known as machine languages, which were specific to a particular type of computer. As computers became more sophisticated, high-level programming languages were developed, such as FORTRAN and COBOL, which were easier for humans to read and write. These languages allow programmers to write code t

Best Programming Languages to Learn in 2022-2023 | Programming Guide For Beginners

Hey everyone ! Today I am going to share with you which programming language you must learn and why you must learn in year 2022. There are so many programmers,coders, computer science students have a Question in their mind , Which programming language I should have to learn ? I am going to answer this very Important Question. Please tune with me till the end. This article will be very helpful for every computer science and coding enthusiast. Let's get started. The world of programming is dynamic. It means the need for language changes according to duration. Every year we see a new language. Everyone gives us feedback on which language we have to learn. But it's not about which language you must've learn , I would like to ask you whats your purpose is in learning a programming language. Why do you want to learn a programming language ? Well you have answers like I want to create softwares , Applications and websites, decentralised apps, cryptocurrency, blockchai

Friendship day 2021 | greetings

Celebrate this year international friendship day with computer programming languages. Send greetings to your friends by writing a block of code using C,C++, python etc. If you and your friends are interested in tech and computer science then it's one of the best way to wish your friends by sending a block of code through whatsapp message, status, instagram post, facebook post and story, instagram reels etc. Now we will discuss how you will write your code to wish your friend on friendship day 2021. Friendship day 2021 greetings using c program. Friendship day 2021 greetings using cplusplus program Friendship day 2021 greetings using python Program. At last we wish you a very happy friendship day to World citizens. Enjoy celebrate this friendship day online ,using digital greeting cards and art. Hope you like our Idea for greetings on this 2021 friendship day