Thread: help with a function

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    10

    help with a function

    Is there a command which it finds the maximum of numbers? like MAX(A,B) in fortran

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    No, but you would write one pretty easily. See here: http://cboard.cprogramming.com/showthread.php?t=96236

    Todd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, I'm pretty sure there is a macro somewhere in a standard header that does MAX(a, b) of integer/float vaues.

    --
    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.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    thanx for the fast replies!i will make a search

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by massimo84 View Post
    Is there a command which it finds the maximum of numbers? like MAX(A,B) in fortran
    You can do this as a macro or as a function. There are downsides to both. The macro will evaluate its arguments multiple times. The function has call overhead. Two versions

    Code:
    #define MY_MAX(a, b) ((a)>=(b)?(a):(b))
    Code:
    int my_max(int a, int b)
    {
        return a>=b?a:b;
    }
    The best of both worlds is an inline function, if your compiler supports it. It is usually a bad idea to name a macro "MAX" or "MIN", since many system headers on various platforms define these macros themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM