Functions Question

This is a discussion on Functions Question within the C Programming forums, part of the General Programming Boards category; Code: /** * RANDOM(high) * Create random number between 0 and {high} * * @param high any number >= 0 ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Functions Question

    Code:
    /**
     * RANDOM(high)
     * Create random number between 0 and {high}
     *
     * @param high any number >= 0
     */
    Code:
    /**
     * RANDOM_RANGE(low, high)
     * Create random number between {low} and {high}
     *
     * @param low  any number
     * @param high any number >= {low}
     */
    .
    .
    Which is better and why?

    Code:
    #define RANDOM_RANGE(low, high) (rand() % (high - low + 1) + low)
    #define RANDOM(high)            (rand() % (high + 1))
    Or

    Code:
    #define RANDOM_RANGE(low, high) (rand() % (high - low + 1) + low)
    #define RANDOM(high)            RANDOM_RANGE(0, high)
    How about if this kind of coding applied to functions?
    Does it affect performance/code size?

    Thanks in advance.
    Last edited by audinue; 01-09-2009 at 08:14 AM.
    Just GET it OFF out my mind!!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    RANDOM just produces a number between 0 and High.
    RANDOM_RANGE produces a number in the range low to high.
    Whichever you need.
    Although, in unoptimized form, RANDOM is faster (less operations).
    In optimized code, I do not know if it matters.
    Still, performance is negligible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Constant folding is one of the first things that an optimizing compiler does - constant folding is where it "computes the result of multiple constants".

    Code:
    #define RANDOM_RANGE(low, high) (rand() % (high - low + 1) + low)
    #define RANDOM(high)            (rand() % (high + 1))
    Code:
    #define RANDOM(high)            RANDOM_RANGE(0, high)
    So the compiled code will be identical, since low = 0 means that all that remains of the calculation is the same as your other macro (assuming optimization is enabled - discussing the optimization, code efficiency/size or other such without optimization is meaningless).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 06:00 AM
  2. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-22-2008, 11:38 PM
  3. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  4. Question about functions
    By richdb in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 12:18 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21