answersLogoWhite

0


Best Answer

Subroutine:

1.When a task is to be done repeatedly then it is written as subroutine and this subroutine will be called each time to perform that task.

2.Subroutine program will be stored in some memory location and program control will be transfered to that location each time.

3.where as in macro the number of instructions will be less than subroutine.Here each time u call a macro that set of instructions will be inserted in that location.

4.macro doesn't have return statement while subroutine has.

5.memory requirement for macro is higher.

6.execution time of macro is lesser than subroutine.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Macros are essentially shorthand representations of arbitrary sections of the source code, which makes the source code succinct, while its (the macro template's) expansion replaces each of its presence prior to compilation. Whatever is there to do with Macros, it is done by the preprocessor, so that the source code is ready for compilation. Function is a calling routine, whence a large program is divided into separate portions, each portion doing a separate job, and proper calling of these portions in different places combines the works done by them into the required complete output. Thus functions have nothing to do with the preprocessing period, they are just compiled. To some extent function and macro is similar, for a macro can occasionally be invoked to perform a task that is generally entrusted to a function. But the similarity ends there.

The differences are:
  1. Macro consumes less time. When a function is called, arguments have to be passed to it, those arguments are accepted by corresponding dummy variables in the function, they are processed, and finally the function returns a value that is assigned to a variable (except for a void function). If a function is invoked a number of times, the times add up, and compilation is delayed. On the other hand, the macro expansion had already taken place and replaced each occurrence of the macro in the source code before the source code starts compiling, so it requires no additional time to execute.
  2. Function consumes less memory. While a function replete with macros may look succinct on surface, prior to compilation, all the macro-presences are replaced by their corresponding macro expansions, which consumes considerable memory. On the other hand, even if a function is invoked 100 times, it still occupies the same space. Hence function is more amenable to less memory requirements.
This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Macros are a text-replacement system. They allow you to symbolise commonly used definitions, which can help simplify your code, but at the expense of type-safety, code size/performance, hidden side-effects and portability.

Consider the following:

#define MAX(X,Y)((X)>(Y)?(X):(Y))

template <class T>

T Max(T x, T x){ return( x>y?x:y ); }

In both cases you can call either MAX() or Max() to compare any two values. However, since the macro is not type-safe, there's no telling what the return type will be if X and Y are not intrinsically the same type. Nor can the compiler determine if an appropriate operator overload exists to compare X and Y correctly. Other side-effects include the way in which expressions are evaluated within the macro. Try MAX(a++,b++) and watch the largest value increment twice!

In this example, the function template is the preferred method. This ensures that both x and y are of the same intrinsic type, that a suitable operator overload exists to compare them, and that expressions are correctly evaluated once and only once.

Templates can be inline expanded just like a macro, but the compiler is free to ignore inline expansion if there would be no benefit. Macros are inline expanded whether you like it or not, and it's not always beneficial; larger code reduces overall performance.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A macro is simply a text-substitution mechanism. You use the #define directive to define both macro symbols and precompiler directives. Every occurrence of a macro symbol within your code is substituted with the macro definition during preprocessing.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The terms macro and template have many different meanings in different contexts. For example, within Microsoft Word, a template is a blank design of a document such as an empty letter with letterhead and predefined formatting preferences, while a macro is a small program used to automate repeated complex tasks.

In the C Programming language, the word macro refers to an expanding pre-processor symbol, also sometimes known as a pound-define. These definitions provide a simple one-for-one text replacement method which can be parameterized through macro arguments.

For example, the following macro determines the smallest value of two arguments. This definition saves writing the same ternary expression all over again, which saves time, makes the code more readable, and prevents errors:

#define MIN(a,b) ((a) < (b) ? (a) : (b))

Notice the use of parentheses. While not strictly necessary in many cases, they can prevent unintended errors when expanding the macro, subject to the chosen arguments a and b.

The C programming language does not support templates, but the C++ language does. In C++, templates are generic class definitions, which are parameterized to support a particular data type at the time of instantiation. This allows for the definition of generic and reusable data structures and algorithms, which can then be used in a type-safe manner. For example, the following code snippet creates a stack object for "int" elements, based on a hypothetical Stack template:

Stack stack;

Type safety means that the object "stack" implements its internal data storage and operations based on the chosen type (int in this example), as opposed to a generic one-for-all data type. Type-safety promotes efficient code and promotes detection of coding errors at compile-time.

Therefore, the answer to the question what is the difference between a template and a macro in the C programming language is that macros do and templates do not exist in the C programming language. In C++, both macros and templates exist, but are generally very different tools, and comparing them means comparing apples with Pears.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

In application programming, macro and function(or: subroutine, etc) are often used to describe the same thing: a piece of code that can be written and tested once, then used over and over again.

In system programming, a macro generally is different, although not without similarity to a function (or subroutine, etc): A function is written once and its code exists once. Whenever the function is called, the processor actually branches (jumps) into the function's code, executes that code, and returns to the location just after the call. A macro is also written once, but whenever its is referenced in the source code, a parameterized copy is expanded at compile or assembly time. Unlike the function, the macro thus exists as many times as it is referenced, possibly in slightly different versions thanks to parameterization.

In comparison, macros generally consume more code memory but execute faster than functions (because they don't incur the overhead of parameter transfer, call and return from a function). In systems programming, macros are typically only used for very short sections of repeated code which are time-critical or don't justify the function call overhead.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Macros and functions are entirely
different. A macro is, in principle, text processing. Like "Find and
replace" in your text editor. Only it happens automatically in the
compiling process, after tokenisation but before lexical analysis.
Functions, OTOH, are genuine C language constructs and not merely text
processing features.
Functions are handled in all states of the compiling process, right up
to linking. The generated object files make a distinction between each
function, and the linker then links all calls between functions
together. In some cases, even the run-time program knows which function
it's currently executing.
None of this is the case with a macro. Once the preprocessor has
expanded the macro, the rest of the process is handled as if the macro
never existed.
Type your answer here...?

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A macro is a text-replacement system used to modify your code prior to compilation. For instance, by defining a macro X with the value 42, every instance of X within your code will be replaced by the literal value 42. This is a bit like defining a static constant, but whereas there is only ever one instance of a static constant, there can be many instances of a literal throughout your program. However, unlike a static constant, a macro is not type safe.

Since macros are nothing more than text replacements, you can define virtually anything with a macro, including complex functions and data structures that might otherwise be difficult to implement in C++ alone. However, since macros are preprocessed, the compiler cannot help you debug macros. By the time the compiler sees your code, it's actually looking at the intermediate code that was emitted by the preprocessor; the macros no longer exist at that point because the text they contained is literally embedded in your source.

A function, on the other hand, conforms to C++ type safety and can be easily debugged by the compiler. Template functions make it easier to overload your functions to cater for different data types in a type-safe manner, thus the need for macro functions is lessened. Macros have their uses -- the built-in debug macros, such as ASSERT() and TRACE(), are extremely useful as they reduce to nothing in release builds -- but macros in general should be avoided whenever it is possible or practical to do so. The more rigidly you stick to native C++ code, the more robust your code will be in the long run, as you not only guarantee type safety, you enlist the compiler to help you debug your code. Macros are difficult to maintain and will generally cause far more problems than they are actually worth. If you must use a macro, do try and keep it as simple as possible, and think carefully about why the C++ method is unsuitable.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the differences between Macros and Functions in C Language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point. Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself. Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.


Elements of assembly language programming What is A simple assembly scheme pass structure of assembler design of two pass assemblers a single pass assemblers what is macros?

what are the elements of assembly language programming?


Most c plus plus programmers use uppercase letters for variable names true or false?

False. Most C++ programmers use uppercase for macros (precompiler definitions), making them less likely to be confused with actual variables, constants or functions in the C++ source code. Macros are not actually part of the C++ language because the compiler never sees them, but they allow the precompiler to perform preprocessing tasks that would be difficult or impossible to accomplish with C++ code alone.


Assembly language to machine language translation is?

Assembly language to machine code translation is a "one to one" translation process, as every individual instruction expressed in the assembly language corresponds to exactly one machine instruction. Note this does not hold for pseudo instructions or expanding macros, which are supported by some assemblers.


What are the difference between template and macro?

They both do pretty much the same thing: they both generate code. However, macros are not type-safe, they aren't even type-aware. So if a function body has to generate a type from its argument, then you cannot use a macro, you must use a template. Even if that is not an issue, the lack of type-safety is a major drawback in a strongly-typed language like C++. Remember, macros are not part of the language itself, they simply exist to augment the language by providing simple text replacements, ultimately making code easier to read and understand. Using them in place of function or class templates is never a good idea no matter how simple the function or class. A bit of extra typing can save a mountain of headaches. The biggest problem with macros (besides their lack of type awareness) is that they are extremely difficult to debug because they are inline expanded by the preprocessor. This means that if the compiler detects and error, it can only do so in the expanded code which simply doesn't exist in your source code. This also means you cannot set breakpoints in a macro because the macro won't physically exist in the preprocessed code. Macros can be used to generate any code except the definition of another macro. This can be extremely useful and is extremely powerful when used appropriately. However, templates can only generate explicit dynamic data types and explicit function overloads. A macro that merely emulates a template is therefore not a particularly useful macro, it is an abhorrent abuse of the language and its compiler. The compiler is there to help you so you are encouraged to enlist it as much as possible. The min/max macros are often quoted as being simpler and easier to use and understand than their equivalent template functions. However, they actually demonstrate why macro functions are so bad in the first place. Calling max(++a, b++) will quickly dispel any myths about macros being easier to work with. Macros completely ignore namespaces and scope, which can result in name clashes that will produce some really funky code. By contrast, templates are fully namespace and scope aware.

Related questions

Which is preferred to use in a program Macros or functions?

Definitely functions, since they provide type safety. Many macros also generate unwanted behavior, because of the way they're expanded. In my opinion, just use inline functions, but if it seems appropriate to use macros, use them -- but do so, with caution.


What types of macros are available in Flowcode?

Component Macros are built in functions used to control components such as an LCD display, EEPROM, switches, serial data, etc. Macros are user created functions that help make the program more modular. As far as I can tell, there are only those 2 types.


Which header files contain declaration for the files input and output functions and the macros defined and that are used with file input and output functions?

stdio.h


How marketing functions can be shifted and shared?

Marketing functions for macros cannot be shifted or shared. They must all be processed by someone and none can be left out.


What are the icons in Microsoft Word 2007 and their functions?

Word 2007 file extensions have been changed as noted below :Type FileWord 2007 document without macros .docxWord 2007 document with macros .docmWord 2007 Template without macros .dotxWord 2007 Template with macros .dotm


What is the difference between macros in Microsoft Word and Microsoft Excel?

Not much, except that the macros would be written to perform different tasks, because Word (word processor) and Excel (spreadsheet) have different functions. Both are written by the user to perform a specific task, but since each application handles most tasks differently, the macros would need to be designed for the tasks relevant to the specific application.


What is the difference between macros and wizard?

a wizard inserts fields and a macros is something like a hyperlink that when you click on it, it automatically takes you to another page!


What are the complex technical functions offered by the word processing package?

form fields, linking, macros, table of contents, styles


Can you leave blank lines between macros in a macro group?

No, because Access will interpret a blank line as no further action and stop executing the macros.


Will openoffice crash with macros?

The honest answer is: "OpenOffice.org is software and therefore can crash as any software." However, the more interesting question is does OpenOffice.org support macros? The answer is: Yes and No! OpenOffice.org does support its own macro language. However the standard edition (as of release 2.3) does not support Visual Basic Macros, as used in Microsoft Office. There are editions of OpenOffice, that support VBA macros.


Why you use ctype.h in c language?

it contains the information used by character classification and character conversion macros


What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point. Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself. Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.