This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiling only the code under IF part in C

Hi All,

Till now I have written code in assembly language for 8051 controller. Now I would like to write code in C. Here I have a problem. I will explain it clearly.

When I write code in assembly, I used to write assembler directives. In that there is a provision to compile only the if part like below:

In INITPARA.INC (contains all the parameters), i will declare

VER1_0: EQU NO
VER1_1: EQU YES

In other ASM program, I will write like below:

IF(VER1_1 = YES) THEN STATEMENT 0 STATEMENT 1 STATEMENT 2
ELSE STATEMENT 3
ENDIF

Now, when I compile, only code under IF part i.e., STATEMENT 0, STATEMENT 1, STATEMENT 2 are compiled. If I equate VER1_0 to YES and VER1_1 to NO in INITPARA.INC then only code under ELSE part is compiled (STATMENT 3). This is a very good feature.

Is it possible to do the same in C. If not, size of my hex file will be increasing by every version

regards,
Bhavani Shankar S

  • You need a goot book about C.

    Such a book would also include a chapter about the preprocessor, which do allow conditional compilation.

  • "Now I would like to write code in C"

    Then, as Per says, you need to take some time to learn the 'C' programming language.

    Simply trying to jump in and hack away at some code is not usually a terribly effective way to learn a programming language. You at least need to get yourself a good book (or books) and, in my opinion, preferably take a 'C' programming class.

    For some books, see: http://www.keil.com/books/

    "Is it possible to do the same in C."

    Yes, it is - and this is one of the things that you would learn from your book(s) and/or class!

    Alternatively, if you routinely have projects where some parts are not required, it might be more appropriate to structure your projects into separate modules, and only build the ones that are actually required...