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

Super ascii string cost, want the codings for these topic in ur experts min...

want the codings for these topic in ur experts minds.com

C++, padovan string c++ program

padovan string c++ program

Define register simply with bit fields, Define register with bit fields? ...

Define register with bit fields? We could define register simply with bit fields: struct DISK_REGISTER { unsigned ready:1; unsigned error_occured:1; unsigned disk_spinni

Padovan string, write a C program for padovan string for a natural number ...

write a C program for padovan string for a natural number   program 1 : package test.padovanstring; public class PadovanString {     public int stringOccurrences(int n,

Model view controler, store the name of the political parties and the corre...

store the name of the political parties and the corresponding votes in two lists of equal length. access the lists is provided by two methods, each of which creates an interator th

Storing street addresses with doubly linked lists, Write a C++ program with...

Write a C++ program with header and source files to store street addresses using the Doubly Linked List ADT. Modify the Node class from Lab Assignment 3 so that it becomes a node i

How must runtime errors be handled in c++, How must runtime errors be handl...

How must runtime errors be handled in C++ - Runtime errors in C++ can be handled using exceptions. - This exception handling mechanism in C++ is developed to handle errors i

When should i use references, A: Use references when you can use, and use p...

A: Use references when you can use, and use pointers when you have to. References are generally preferred over pointers whenever you don't require "reseating". Usually this mean

Simple object-oriented program, This assignment is to be undertaken individ...

This assignment is to be undertaken individually - no group work is permitted. Background information This assignment is an exercise in simple object-oriented programming and, acco

Electrical eng, calculate the total resisitance of a series circuit?

calculate the total resisitance of a series circuit?

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