EBK DATA STRUCTURES AND ALGORITHMS IN C
EBK DATA STRUCTURES AND ALGORITHMS IN C
4th Edition
ISBN: 9781285415017
Author: DROZDEK
Publisher: YUZU
bartleby

Concept explainers

Question
Book Icon
Chapter 1, Problem 7E
Program Plan Intro

Templates and Macros:

Macros:

  • A macro can be stated as a string that compiler would replace with the defined value.
    • Example: #define STRING "Value"
  • Macros are not safe type, that is, float data is not accepted by a macro that is defined for integer operations. Type checking is not performed while expansion.
  • Finding errors in macros is difficult.
  • When a variable is post incremented or decremented, operation is been performed twice.

Templates:

  • A template is a way of making functions with independency in data type. Macros could not accomplish this task.
    • Example: A sorting function does not bother the type of data it is sorting, since the applied algorithm is same.

Syntax for Templates and Macros:

The syntax for template definition is shown below

Syntax for template:

template <class type>

type class-name<type>:: macro_name(arguments)

{

}

The syntax for macro definition is shown below

Syntax for macro:

#define macro_name replacement-text

return-type main()

{

  Variable-name = macro_name(arguments);

}

Expansion is performed twice for the above macro. It denotes a limitation of macros. All occurrences of macro in file would be replaced by “replacement-text” before compilation of program. The template removes the macro limitations.

Blurred answer
Students have asked these similar questions
What is the use of generic pointers in c?discuss with the help of an example?
Why there is no virtual constructor in C++? Illustrate with example.
Define a class template. When would a programmer want to use class templates in C++?
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education