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

Computer Science 101, This is what I need help with. "Create a program cre...

This is what I need help with. "Create a program creates an interface allowing the user to select from some other programs I have made one being a money converter and the other ca

Stack over flow for sun sparc, Please use C or C++ to write your programs. ...

Please use C or C++ to write your programs. Our homework will focus on SUN sparc machines because to exploit x86 stack-overflow is too easy. This is not just a programming assignme

C program to find vowels and remove vowels , C Program to FIND VOWELS AND R...

C Program to FIND VOWELS AND REMOVE VOWELS void main() {           char s[100],s1[100];           int i,j,k=0;           clrscr();           printf("ENTER ANY S

Parking garage, A parking garage charges a $2.00 minimum fee to park for up...

A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $0.50 per hour for each hour or part thereof over three hours. The maximum charge for a

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

File processing, Write a C++ program doing the following: The program reads...

Write a C++ program doing the following: The program reads from the keyboard the numbers of rows and columns of a 2-D array of integers. Then, it allocates this array (let’s name i

Distinguish between a class and an object, Problem: (a) Distinguish bet...

Problem: (a) Distinguish between a class and an object. (b) Define instantiation. (c) What is the importance of the "this" variable in java. (d) What is encapsulation?

Explain briefly about class and objects, Question 1. Explain Briefly ab...

Question 1. Explain Briefly about class and objects. 2. Create a class string which stores a string value. Overload ++ operator which converts the characters of the string t

Need android app development, Project Description: I am seeking a develo...

Project Description: I am seeking a developer who can start an app from scratch and get it delivered to me as soon as possible. It is a little android based app. A background on

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