Thread: Conditional Compile Question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Conditional Compile Question

    Just a quick basic question for a C newb:

    If I use some preprocessor conditional compile directives, can I declare the same variable to be two different values? For example, I want an array to have 7 elements if one #ifdef is true, and 9 elements if another #ifdef is true, with each containing different values. If the arrays are initialized within the #ifdefs, is this legal, or will I get some sort of "Redefinition" error with my compiler (it's Keil C51 if that matters)?

    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #ifdef FOO
    #define BAR 7
    #else
    #define BAR 9
    #endif
    int array[BAR];
    Yeah, it's fine. You could also move the declaration of the array in there also. But in either case, for looping and what not, you'll want to do the '#define BAR X' so all of your loops can be:
    Code:
    for( x = 0; x < BAR; x++ )
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Preproccessor conditional compilation
    By Garfield in forum C Programming
    Replies: 5
    Last Post: 09-28-2001, 09:28 AM