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

Define some important point for bit fields with structures, Define some Imp...

Define some Important Point for bit fields with structures? Only n lower bits will be assigned to an n bit number thus type cannot take values larger than 15 (4 bits long)

Program, Byteland county is very famous for luminous jewels. Luminous jewel...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Binary search tree is constructed by repeatedly, Assume that a Binary Searc...

Assume that a Binary Search Tree is constructed by repeatedly inserting exact values in to the tree. Argue that the number of nodes examined in searching for a value in the tree is

Program to create a query from database, Implement (a part of) electronic e...

Implement (a part of) electronic election Scenario where following political parties are participating in election: PPP, PML and PTI.  Create a class PollingStation where it stor

Need to fix a code written in c++, Need to Fix a code written in c++,SDL2.0...

Need to Fix a code written in c++,SDL2.00 for high CPU and memory leak There is a 2D simple game written with SDL2.00 using c++. The game isn't complete and the code is a little

OpenGL configured environment, 1. Using Visual C++ and your OpenGL co...

1. Using Visual C++ and your OpenGL configured environment, write an application that displays a “unique” graphical scene that you designed and coded for this course. What yo

Define the keywords of c language, Define the Keywords of c language? C...

Define the Keywords of c language? C keeps a small set of keywords for its personal use. These keywords can't be used as identifiers in the program. Here is the list of keyword

The Shell or Command Line Interpreter is the fundamental Use, 1. The shell ...

1. The shell must support the following internal commands: i. cd - Change the current default directory to . If the argument is not present, report the current directory. If t

DECODE, Smugglers are becoming very smart day by day. Now they have develop...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

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