Declaring ULONG_MAX in C

Manav Dubey - Streetsville SS  picture Manav Dubey - Streetsville SS · Apr 4, 2017 · Viewed 7.7k times · Source

I have been doing a lot of research on ULONG_MAX and trying to find out how to declare it, but it doesn't seem to work. I using the header #include <stdio.h> and #include <limits.h to make ULONG_MAX work. There is nothing much about ULONG_MAX online, so that is making it difficult for me to use it. I feel like I am using ULONG_MAX wrong.

Question: How to declare ULONG_MAX

Sample Code:

#include <stdio.h>
#include <limits.h>

main(){
  ULONG_MAX number;

  return 0;
}

Error:

gcc version 4.6.3

main.c:4:1: warning: return type defaults to 'int' [-Wimplicit-int]
 main(){
 ^~~~
main.c: In function 'main':
main.c:5:13: error: expected ';' before 'number'
   ULONG_MAX number;
             ^~~~~~
main.c:6:3: error: 'num' undeclared (first use in this function)
   num = ULONG_MAX;
   ^~~
main.c:6:3: note: each undeclared identifier is reported only once for each function it appears in

exit status 1

The compiler I am using: Repl.it

Answer

paxdiablo picture paxdiablo · Apr 4, 2017

ULONG_MAX is not a type, it's the maximum value allowed for an unsigned long type, typically defined as something like:

#define ULONG_MAX 0xFFFFFFFFUL

So, semantically, there is no difference between:

ULONG_MAX number;

and the clearly incorrect:

42 number;

In order to use the value, you would do something like:

unsigned long bigVal = ULONG_MAX;