Thread: macro to determine if variable is signed/unsigned

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    macro to determine if variable is signed/unsigned

    #define IS_UNSIGNED( var ) \
    ( (var) >= 0 && ~(var) >= 0 )

    what are the forseen problems in using this? I see a problem when looking at zero after the integer is one's complemented.

    Can this be modified to accept a variable that is a type, as below?

    #define IS_UNSIGNED( TYPE ) \

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why the fascination with macros - both your threads have been about macros.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Yeah, a function would be a whole lot better.

    Why don't you just test it? Try it out on 0, 1, -1, and the maxima for integers.

    I don't quite see the need for this function, however. C is a strongly typed language, so the type of a variable shouldn't be ambiguous.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Smile

    Hey Sal, I don't have a fixation on macros, I am just trying familiarize myself with debugging and ..... I was told this will be helpful on some of new projects- in lieu of some systems work.
    Last edited by Cdigitproc1; 04-29-2005 at 11:47 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Macros cannot be debugged is one thing you should be learning then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Wink

    Thx Sal.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't know about other compilers, but I know gcc provides a means to see your source code after the preprocessor stage but before the compilation stage. You just use the -E switch:
    Code:
    itsme@dreams:~/C$ cat preproc.c
    #define MIN(x,y) (((x) > (y)) ? (y) : (x))
    
    int main(void)
    {
      int a = 5;
      int b = 7;
      int c;
    
      c = MIN(a, b);
    
      return 0;
    }
    Code:
    itsme@dreams:~/C$ gcc -Wall -E preproc.c
    # 1 "preproc.c"
    
    
    int main(void)
    {
      int a = 5;
      int b = 7;
      int c;
    
      c = ((( a ) > (  b )) ? (  b ) : ( a )) ;
    
      return 0;
    }
    itsme@dreams:~/C$
    Maybe you'll find that helpful.
    If you understand what you're doing, you're not learning anything.

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    cpp -E in most unixen does the same thing as gcc -E or cc -E - it runs the precompiler and redirects output to stdout.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  4. The Difference Between A Macro And A Variable?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 12:26 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM