Saturday 16 June 2012

COMPLICATED PARAMETER PASSING IN #DEFINEs

If you are familiar with this term then you are surely a good programmers,
it is often slammed upon us to use it in our programs.

Why is it so important, well uses of this so called preprocessor are infinite.

from basic macro substitution to, case based selection,

and even some combined with ternary operators ( ? : ).

At this point everything moves well for every most of us,

but then its a pain in the place we hate the most as soon as we meet these type of declarations.
If have seen some really good programmers fall for this type of confusion.

--------------------------------------------------------------------------------------------------------------------------------------

#define true_statement(num) printf(#num " = %d\n", num)

--------------------------------------------------------------------------------------------------------------------------------------

For many of people, its like how the f@#$ another pound symbol got in thr,
this goes for guys learning programming online because 70-80% sites teaching C language are not covering up this minute detail.

Documentation for this type of usage can be acknowledged
 from the very famous "THE C PROGRAMMING LANGUAGE" by Kernighan and Ritchie.

As taken from their book
"formal parameters are not replaced within quoted strings. If, however, a parameter name is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replace by the actual argument"

So
------------------------------------------------------------------------------------------------------------------------------------
//when invoked by num = 2

macro gets expanded into

printf("2" " = %d\n" , 2);

As we know the nature of "" " in C it obviously gets concatenated.

so end the result is

printf("2 = %d\n" , 2);


-------------------------------------------------------------------------------------------------------------------------------------

The example shown above is for integer type,
its common that another thought may arise in many of the readers

How does it manages special characters then??

These are the characters that must have crossed your mind, because these
are the ones that can lead to illegal string.

 =  "
 =  \

You dont need to worry about it, it gets fixed within the actual argument only,

" gets converted to "\ and each \ get converted to \\,
so string is still legal.


These types of reference are rarely used in common practices,
most common reason to avoid this is incapability of understanding for a greater mass.

That was still a rare though still can be found type of example,
but this is totally like once in a decade issue.

-------------------------------------------------------------------------------------------------------------------------------------
 #define two_digit(tenth_place,unit_place) tenth_place##unit_place
 -------------------------------------------------------------------------------------------------------------------------------------

One pound symbol was enough to create enough confusion, but now here we have two of them, but dont get fooled by the look, its rather easy to understand than the previous one.

Parameters passed through gets concatenated with each other.

## cause all white spaces to be removed surrounding it and if there lies a parameter adjacent to it, it replaces it with the actual parameter values.

Eg.
-------------------------------------------------------------------------------------------------------------------------------------
two_digit(1,2)

will result into

12
-------------------------------------------------------------------------------------------------------------------------------------

i hope you enjoyed reading this article,

Thanks for reading.
B-)

No comments:

Post a Comment