#include<stdio.h>
int main() { int(a); a = 42; printf("Value in a = %d\n",a);
return 0; }
using macro...
http://www.cprogramming.com/tutorial/cpreprocessor.html
http://stackoverflow.com/questions/1358232/why-use-macros-in-c
http://en.wikipedia.org/wiki/C_preprocessor Macros: Macros, which are special lines/fragments of code, differ from any other
code written in the source files in the aspect that it's not passed to
the compiler (either for the purpose of encoding into machine
instructions, or some intermediate form instructions). Instead, macros
perform textual manipulations on the source code before passing it to
the compiler, where this process preliminary to compiling the source
files is called "preprocessing", and the software component responsible
of doing it is called the "preprocessor". Henceforth, macros are usually
defined as "preprocessor directives", where a segment of code is
replaced by the results of the macro processing before passing the
source code to the compiler. A very valuable technique to gain a
thorough understanding of the operation of macros in any environment is
to learn how to obtained the preprocessed source code, and see how the
macro directives are expanded. Preprocessor macros, because of their
distinctive nature compared to compiled code, are powerful tools in
programming, and that's why the concept of macros is applied in most
programming languages, such as the C programming language, and the
assmebly language (where each assembler provides its own macro
directives). Macros should not be confused with other compiled code,
and they should be used only while bearing in mind how they work, namely
by modifying the source code prior to passing it to the compiler.
Trying to employ macros to do jobs that cannot be done before
compilation, but rather depends on the program's compiled state or the
execution yields is a common mistake that beginners make, mainly those
who learn programming by reading others' code, and since a lot of
programmers use macros excessively for common programming jobs such as
the emulation of constants, it's easy for a beginner to fall into the
trap of thinking that they represent a code that is "executed" by the
processor in run-time. The excessive use of macros for purposes
achieveable by run-time techniques is not recommended, and somehow
deprecated. Nevertheless, macros sometimes are viable tools that has no
other alternative, such as the case with assertions. Asserting is very
common in defensive programming, and means warning the programmer at
run-time if some necessary condition was not met upon execution in the
Debug versions of the program, so that appropriate actions are taken in
the time of development, while completely/or partially disappearing from
the code in the Release version. The problem with the function that is
meant to carry out such assertion is subtle, because, such a function is
sometimes supposed to keep evaluating the expression even in the
Release versions, and, if the expression evaluated to false and this
were the debug version, it should output a message with a reasonable
indication of that failing expression, usually using the textual C code
conveying the expression, as well as the line number at which the
assertion was performed and the code file name. This very interesting
information CANNOT be obtained, in any means, at execution time, and
such functionality is only achieveable via using simple -though
powerful, macros. The macro used will take the bare expression and put
it in an if statement, then within the statement block it will enclose
the expression in double quotations to yield the representative string,
while inserting the line number at which the assertion line was
mentioned in the source file, plus the source file name, using special
macro directives that are simply substituted with such information
before compilation. To learn more about C macros, refer to the famous
Kernoghan/Richie's book "The ANSI C Programming Language", the
explanations are brief but thorough.
|