
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What are macros in C programming language?
Macro substitution is a mechanism that provides a string substitution. It can be achieved through "#deifne".
It is used to replace the first part with the second part of the macro definition, before the execution of the program.
The first object may be a function type or an object.
Syntax
The syntax for macros is as follows −
#define first_part second_part
Program
In the program for every occurrence of first_part is replaced with the second_part throughout the code.
#include<stdio.h> #define square(a) a*a int main(){ int b,c; printf("enter b element:"); scanf("%d",&b); c=square(b);//replaces c=b*b before execution of program printf("%d",c); return 0; }
Output
You will see the following output −
enter b element:4 16
Consider another program that explains the functioning of macros.
#include<stdio.h> #define equation (a*b)+c int main(){ int a,b,c,d; printf("enter a,b,c elements:"); scanf("%d %d %d",&a,&b,&c); d=equation;//replaces d=(a*b)+c before execution of program printf("%d",d); return 0; }
Output
You will see the following output −
enter a,b,c elements: 4 7 9 37
- Related Articles
- What are the advantages of C++ Programming Language?
- What is C++ programming language?
- Why files are needed in C programming language?
- Comments in C++ Programming Language
- C++ Programming Language Features
- C Programming Language Standard
- What is Programming Language?
- What are the platforms that support Java programming language?
- Basics of C++ Programming Language?
- A C Programming Language Puzzle?
- Limitations of C programming language
- What is Java programming language?
- What is Perl Programming Language?
- What are the effects of language design in the programming environment?
- Hygienic Macros in C

Advertisements