Wednesday, February 25, 2015

How can type-insensitive macros be created?

Question:How can type-insensitive macros be created??

Answer: </I>A type-insensitive macro is a macro that performs the same basic operation on different data types. This task can be accomplished by using the concatenation operator to create a call to a type-sensitive function based on the parameter passed to the macro. The following program provides an example: #include &lt;stdio.h&gt; #define SORT(data_type) sort_ ## data_type void sort_int(int** i); void sort_long(long** l); void sort_float(float** f); void sort_string(char** s); void main(void); void main(void) { int** ip; long** lp; float** fp; char** cp; ... sort(int)(ip); sort(long)(lp); sort(float)(fp); sort(char)(cp); ... } This program contains four functions to sort four different data types: int, long, float, and string (notice that only the function prototypes are included for brevity). A macro named SORT was created to take the data type passed to the macro and combine it with the sort_ string to form a valid function call that is appropriate for the data type being sorted. Thus, the string sort(int)(ip);&nbsp; translates into sort_int(ip); after being run through the preprocessor. &nbsp;

Tags:Interview Questions, Languages, C,

Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.



Sponsor Advertisement



Library Automation Software

Library Management System Software

Our Library Management Softare offers complete library management modules and automation solution that enables information providers, information managers, resource incharge, resource managers & librarians to manage & disseminate information & resources....

No comments:

Post a Comment