MIN and MAX in C

后端 未结 14 1561
攒了一身酷
攒了一身酷 2020-11-22 13:32

Where are MIN and MAX defined in C, if at all?

What is the best way to implement these, as generically and type safely as possible? (Compil

14条回答
  •  深忆病人
    2020-11-22 14:36

    It's also provided in the GNU libc (Linux) and FreeBSD versions of sys/param.h, and has the definition provided by dreamlax.


    On Debian:

    $ uname -sr
    Linux 2.6.11
    
    $ cat /etc/debian_version
    5.0.2
    
    $ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
    #define MIN(a,b) (((a)<(b))?(a):(b))
    #define MAX(a,b) (((a)>(b))?(a):(b))
    
    $ head -n 2 /usr/include/sys/param.h | grep GNU
    This file is part of the GNU C Library.
    

    On FreeBSD:

    $ uname -sr
    FreeBSD 5.5-STABLE
    
    $ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
    #define MIN(a,b) (((a)<(b))?(a):(b))
    #define MAX(a,b) (((a)>(b))?(a):(b))
    

    The source repositories are here:

    • GNU C Library
    • FreeBSD

提交回复
热议问题