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 9234333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:45:56+00:00 2026-06-18T06:45:56+00:00

When I read someone’s code I find that he bothered to write an explicite

  • 0

When I read someone’s code I find that he bothered to write an explicite type cast.

 #define ULONG_MAX ((unsigned long int) ~(unsigned long int) 0)

When I write code

  1 #include<stdio.h>
  2 int main(void)
  3 {
  4         unsigned long int max;
  5         max = ~(unsigned long int)0;
  6         printf("%lx",max);
  7         return 0;
  8 }

it works as well. Is it just a meaningless coding style?

  • 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-06-18T06:45:57+00:00Added an answer on June 18, 2026 at 6:45 am

    The code you read is very bad, for several reasons.

    • First of all user code should never define ULONG_MAX. This is a reserved identifier and must be provided by the compiler implementation.

    • That definition is not suitable for use in a preprocessor #if. The _MAX macros for the basic integer types must be usable there.

    • (unsigned long)0 is just crap. Everybody should just use 0UL, unless you know that you have a compiler that is not compliant with all the recent C standards with that respect. (I don’t know of any.)

    • Even ~0UL should not be used for that value, since unsigned long may (theoretically) have padding bits. -1UL is more appropriate, because it doesn’t deal with the bit pattern of the value. It uses the guaranteed arithmetic properties of unsigned integer types. -1 will always be the maximum value of an unsigned type. So ~ may only be used in a context where you are absolutely certain that unsigned long has no padding bits. But as such using it makes no sense. -1 serves better.

    • “recasting” an expression that is known to be unsigned long is just superfluous, as you observed. I can’t imagine any compiler that bugs on that.

    Recasting of expression may make sense when they are used in the preprocessor, but only under very restricted circumstances, and they are interpreted differently, there.

    #if ((uintmax_t)-1UL) == SOMETHING
    ..
    #endif
    

    Here the value on the left evalues to UINTMAX_MAX in the preprocessor and in later compiler phases. So

    #define UINTMAX_MAX ((uintmax_t)-1UL)
    

    would be an appropriate definition for a compiler implementation.

    To see the value for the preprocessor, observe that there (uintmax_t) is not a cast but an unknown identifier token inside () and that it evaluates to 0. The minus sign is then interpreted as binary minus and so we have 0-1UL which is unsigned and thus the max value of the type. But that trick only works if the cast contains a single identifier token, not if it has three as in your example, and if the integer constant has a - or + sign.

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

Sidebar

Related Questions

Does play automatically cache data? I'm trying to read this code that someone else
Can someone please explain how to read the below code to find last column
When reviewing code I came across something odd, someone had read that you can
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Before someone said that I did not read I may say that I read
I've read somewhere that <img> element behaves like both. If correct, could someone please
I read somewhere that someone was going to create a raster engine similar to
Does someone know a library that I can use to read MP3 tags in
//to-long-didnt-read -> missing lgssapi_krb5 library and can't seem to find it i am having
I read some code where someone did this in Ruby: puts ('A'..'Z').to_a.join(',') output: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

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.