Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:35:54+00:00 2026-05-25T11:35:54+00:00

I am learning Expert C Programming by Peter Van Der Linden. In chapter A.6,

  • 0

I am learning “Expert C Programming” by Peter Van Der Linden. In chapter A.6, the writter described how to determine whether a variable is unsigned or not in K&R C.The macro is below:

#define ISUNSIGNED(a) (a>=0 && ~a>=0)

The book is very old, it was first published in 1994! And I have not learned K&R C before.
The question is that how to determine whether a variable is unsigned or not in ANSI C.

I have tried to solve the problem like this. Since “0” is int in ANSI C, and any other number except float, double and long double will be converted to int or unsigned int by Integer Upgrade when compare with 0. So I want to find an edge between unsigned and signed number. When I compare (the edge type)0 with a, the type of a will not be changed. The macro also the model is below:

#define ISUNSIGNED(a) (a>=(the edge type)0 && ~a>=(the edge type)0)

I can not find the edge type, is there anybody can help me solve the problem?
I have changed “number” to “variable” for more accurate expression.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:35:54+00:00Added an answer on May 25, 2026 at 11:35 am

    How this works

    A signed variable has to store its sign in some bit. Usually this is the most significant one, but it could be any of them. An unsigned variable has no sign bits; thus, the lowest value it can hold is 0. This means that for an unsigned variable a, the expression a >= 0 will always be true.

    So we have:

    ( a >= 0 && ~a >= 0 )
    

    If a is unsigned, the first is true (it has to be), and the second is true (because whatever value ~a is, it’s still an unsigned value, so it’s still >= 0). If a is signed, that means that if the sign bit is set, a >= 0 is false (and the expression returns false, stating that this variable has a signed type). If the sign bit isn’t set in a, then when ~a inverts all the bits in a, the sign bit (whichever one it is) has to be set. This means that it has to be a negative number, which means that ~a >= 0 returns false.

    This does rely on the standard integer promotions to work like you’d expect them to.

    How it doesn’t work

    unsigned char x = 1; // or whatever
    
    printf("%s\n", ISUNSIGNED(x) ? "TRUE" : "FALSE"); // prints "FALSE"
    

    As someone else pointed out, unsigned char gets promoted to an int since any value of ~a for an unsigned char a can easily fit in the range of an int. This is arguably a failing in the standard integer promotions (or a failing in the typing of integral literals).

    There might be another implementation of ISUNSIGNED or ISSIGNED somewhere that can overcome this limitation. The P99 macro library has some mind-bending uses of macros, many relying on C99’s variadic macros, but unfortunately the macro to check whether an expression is signed or not (#define SIGNED(expr) ((1 ? -1 : expr) < (1 ? 0 : expr))) succumbs to the same integer promotions. This might be the best you can do (though I suppose it’s better than nothing in the cases where you’ll want it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I'm not Java expert but I'm just programming this java applet game
I am not an expert in PHP, still learning it. I'd like some hints
I'm learning C++ from scratch, and as such I don't have an expert understanding
Learning a lot in my few years of programming that the best projects are
I am current learning Flex 4 programming now and trying to get as much
I have enjoyed learning to use OpenGL under the context of games programming, and
I've been learning Android for about two weeks now (expert in as2/3). I have
I'm on the beginning slope of learning Mac programming and in particular Cocoa. It
I’m learning Cocoa programming, and I’m unable to figure out how to archive and
Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.