Friday, November 8
Macros In C++ By Umair Sajid
Macros In C++ By Umair Sajid
Macros : are pre-processor directives Just As#include<stdio.h>
#include<graphic
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#include<dos.h>
#include<iostream>
but In Above Directive Directs The Compiler to include followings library functions . However We Are Concern with the Macros.
Syntax: #define ADD x+y
Actually They Can Be think As Same As Functions But Just Defined Before Main and with A little difference of Syntax and working. In a Nutshell You can Call Macros as Functions Using As A Variable
Use: Macros In C++ Is Usually Used Where We Required Fast Execution. However The Same Work Can be Done By Using Functions But The Function can pass arguments and require a return_type of the Integers or Result Return.
Lets Come To A Simple Program That prompt the user to enter a number and prints its cube and square using macros.
#include<iostream>
#define SQUARE num*num // MACROS
#define CUBE num*num*num // MACROS
using namespace std;
int main()
{
int num;
cout<<"\n\t\tEnter A Number : "; // prompt to enter a number
cin>>num; // store it in num
cout<<"\n\t\tThe Square of "<<num<<" is : "<<SQUARE<<"\n"; // Just as function call we have used
cout<<"\n\t\tThe Cube of "<<num<<" is :"<<CUBE; // macros
cout<<"\n\n";
cout<<"\n\t\t***THANKYOU PC Umair Sajid ****\n\n";
}
:)
ReplyDeleteMacros is not the only best choice you can use functions too
ReplyDeleteNone of Your Concern Dear . . .
ReplyDeletemacros were available in c/c++ and functions provide much much more flexibility with security, macros can be called by anyone usually
ReplyDeletewhy use macros if we can have functions
ReplyDeletemore posts by umair
ReplyDeletethe heading is written twice, though the blog is engaging
ReplyDeleteare macros performance improvement as well?
ReplyDeletevery good explanation but what is this PC?
ReplyDeletewe need more similar posts
ReplyDeletea high quality post (y) :)
ReplyDeleteSir Umarir news posts ko aanay diya jaye :P
ReplyDelete