Explain the macros, C/C++ Programming

Assignment Help:

Explain the Macros?

Preprocessor' is a translation stage that is applied to your source code before the compiler proper gets its hands on it. Usually the preprocessor performs textual substitutions on your source code. Macro is a kind of preprocessor which replaces instances of one piece of text with another.

A preprocessor line of the form

#define name text

defines a macro with the given name having as its value the given substitute text. After that for the rest of the current source file someplace the preprocessor sees that name, it will replace it with the replacement text. The name follows the similar rules as ordinary identifiers it can contain only letters, digits, and underscores, and may not begin with a digit. Ever since macros behave quite differently from normal variables or functions it is customary to give them names which are all capital letters or at least which begin with a capital letter. The replacement text is able to be absolutely anything--it's not restricted to numbers, or anything, or simple strings.

The mainly common use for macros is to propagate various constants around and to make them more self-documenting. We have been saying things like

char line[100];
...
getline(line, 100);

but this is neither reliable nor readable it's not essentially obvious what all those 100's scattered around the program are and if we ever decide that 100 is too small for the size of the array to hold lines we'll have to remember to change the number in two or more places. A lot better solution is to use a macro:

#define MAXLINE 100
char line[MAXLINE];
...
getline(line, MAXLINE);

Now if we still want to change the size we only must do it in one place. The macro text MAXLINE is known as a symbolic constant.


Related Discussions:- Explain the macros

What do you mean by a sequential access file, What do you mean by a sequent...

What do you mean by a sequential access file? - When writing programs which store and retrieve data in a file, it's possible to designate that file into various forms. - A s

Substr and random pick file from directory, Hello I''m new to programming, ...

Hello I''m new to programming, and I''m making now my 1st program. My question is how to put substr in textbox that question mark should be at the end of sentence? And 2nd question

Big o notation, The probabilistic Hough transform uses random sampling inst...

The probabilistic Hough transform uses random sampling instead of an accumulator array. In this approach the number of random samples r, is not specified in the OpenCV call, but is

Padovan string, padovan string generation till 40

padovan string generation till 40

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

About, How can I learn all programming languages.?

How can I learn all programming languages.?

Determining the monthly payment on a mortgage loan, The following is the fo...

The following is the formula that can be used to complete that calculation: Monthly Payment = Monthly Interest Rate / (1 - (1 + Monthly Interest Rate) -Payment Interval )) *

Program to calculate pie, This problem familiarizes you with using random n...

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of π using a simulation method called "Monte Carlo". The following fi

Pointer declaration for class, P o i n t e r d e c l ...

P o i n t e r d e c l a r a t i o n f o r C l a s s : M m; M * p m; / / C la ss M i s d e c l a r e d a s

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd