Skip to main content

Difference between Macro and Function

In this post, we are going to understand difference between macro and function. But before getting started, I want to explain little bit about macro and function to you in an easy way.

What is a function?

A function can be stated as a group of statements that performs some kind of task. In every C program, you’ll notice at least one function that is main() function. The execution of the program starts from there only. You can divide your code into smaller functions for reducing the size of the body of main() function.

Now let us see the syntax of a function.

return_type function_name(parameter){
    //body of the function
}

What is a macro?

Macros are used to define symbolic constants. It is a preprocessor directive, which means it replaces itself with the value which is given to it. Let us understand it with an example.

#include <stdio.h>
#define X 3     //macro definition

int main(){
    int x = 10;
    printf("%d\n", x + X);    //here the value of 'X' gets replaced by '3' 
}

The output of the above code is:

13

Hope you’ve got an idea of macro and its use.

Difference between Macro and Function

Macro Function
It is not compiled, it is pre-processed. It is not pre-processed but it is compiled.
Macros do not check for compilation error which leads to unexpected output. Function checks for compilation error and there is a less chance of unexpected output.
Code length is increased. Code length remains same.
Macros are faster in execution than function. Functions are bit slower in execution.
Before compilation process the macro name is replaced by the macro value. In a function call, transfer of control takes place.
Macros are useful when a small piece of code used multiple times in a program. Functions are helpful when a large piece of code repeated number of times.

Comment below if you have queries regarding above tutorial for difference between macro and function.

The post Difference between Macro and Function appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/03/difference-between-macro-and-function.html

Comments

Popular posts from this blog

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Experimental: Reducing the size of .NET Core applications with Mono's Linker

The .NET team has built a linker to reduce the size of .NET Core applications. It is built on top of the excellent and battle-tested mono linker . The Xamarin tools also use this linker so it makes sense to try it out and perhaps use it everywhere! "In trivial cases, the linker can reduce the size of applications by 50%. The size wins may be more favorable or more moderate for larger applications. The linker removes code in your application and dependent libraries that are not reached by any code paths. It is effectively an application-specific dead code analysis ." - Using the .NET IL Linker I recently updated a 15 year old .NET 1.1 application to cross-platform .NET Core 2.0 so I thought I'd try this experimental linker on it and see the results. The linker is a tool one can use to only ship the minimal possible IL code and metadata that a set of programs might require to run as opposed to the full libraries. It is used by the various Xamarin products to extract...

Accessibility Insights for the Web and Windows makes accessibility even easier

I recently stumbled upon https://accessibilityinsights.io . There's both a Chrome/ Edge extension and a Windows app, both designed to make it easier to find and fix accessibility issues in your websites and apps. The GitHub for the Accessibility Insights extension for the web is at https://github.com/Microsoft/accessibility-insights-web and they have three trains you can get on: Canary (released continuously) Insider (on feature completion) Production (after validation in Insider) It builds on top of the Deque Axe core engine with a really fresh UI. The "FastPass" found these issues with my podcast site in seconds - which kind of makes me feel bad, but at least I know what's wrong! However, the most impressive visualization in my opinion was the Tab Stop test! See below how it draws clear numbered line segments as you Tab from element. This is a brilliant way to understand exactly how someone without a mouse would move through your site. I can easily s...