39

I'm getting the error

error: 'INT32_MAX' was not declared in this scope

But I have already included

#include <stdint.h>

I am compiling this on (g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44) with the command

g++ -m64 -O3 blah.cpp

Do I need to do anything else to get this to compile? or is there another C++ way to get the constant "INT32_MAX"?

Thanks and let me know if anything is unclear!

jm1234567890
  • 1,612
  • 3
  • 21
  • 29
  • 2
    @WalterTross baaaad baad idea, – IanNorton Jan 08 '14 at 10:17
  • @IanNorton, maybe you are right, but I still have to see a place where INT32_MAX is a different value – Walter Tross Jan 08 '14 at 14:35
  • My Eclipse (Helios)/MinGW setup did not highlight as an error, and I was able to right click and open declaration, but I had to change 'include' to 'include ' in order to stop INT_MAX reporting a 'not declared in this scope' error. – Clarius Feb 25 '17 at 09:55

10 Answers10

50

Quoted from the man page, "C++ implementations should define these macros only when __STDC_LIMIT_MACROS is defined before <stdint.h> is included".

So try:

#define __STDC_LIMIT_MACROS
#include <stdint.h>
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Blindy
  • 65,249
  • 10
  • 91
  • 131
27
 #include <cstdint> //or <stdint.h>
 #include <limits>

 std::numeric_limits<std::int32_t>::max();

Note that <cstdint> is a C++11 header and <stdint.h> is a C header, included for compatibility with C standard library.

Following code works, since C++11.

#include <iostream>
#include <limits>
#include <cstdint>

struct X 
{ 
    static constexpr std::int32_t i = std::numeric_limits<std::int32_t>::max(); 
};

int main()
{
    switch(std::numeric_limits<std::int32_t>::max()) { 
       case std::numeric_limits<std::int32_t>::max():
           std::cout << "this code works thanks to constexpr\n";
           break;
    }
    return EXIT_SUCCESS;
}

http://coliru.stacked-crooked.com/a/4a33984ede3f2f7e

mip
  • 8,355
  • 6
  • 53
  • 72
  • 5
    Note that this isn't equivalent as it can't be used as a compile-time constant (pre-C++0x that is). – Georg Fritzsche Jul 12 '10 at 23:43
  • 1
    yes! This is what I am looking for, a C++ way to do it. Thanks – jm1234567890 Jul 12 '10 at 23:44
  • @Georg Fritzsche: it can be used as compile-time constant. `const int MY_INT32_MAX = std::numeric_limits::max();` will work just fine. This method is so simple that it will be optimized out as it would be explicitly defined constant. – mip Jul 12 '10 at 23:48
  • 1
    If you use `` and compile with `-std=c++0x`, `INT32_MAX` is defined as a constant anyway (At least over here it is [GCC 4.4.4]). – tjm Jul 12 '10 at 23:53
  • 5
    Doc, its not a *compile-time* constant - you can't use that where the language requires an *integral constant*. Try `switch(1) { case std::numeric_limits::max(): }` or `struct X { static const int i = std::numeric_limits::max(); };`. I'm btw not saying `` is bad, just pointing out a difference. – Georg Fritzsche Jul 13 '10 at 01:50
  • @GeorgFritzsche you were right, but this should be revised, cause C++0x does have `constexpr` and your examples should work now. – mip Mar 26 '12 at 12:49
  • 2
    Pedantic: need to qualify `std::int32_t`, since the `.h` wasn't included and for the sake of good practice in general. – Potatoswatter Jul 22 '15 at 14:55
8

Hm... All I needed to do was #include <climits> nothing else on this page worked for me.

Granted, I was trying to use INT_MIN.

pixelpax
  • 1,435
  • 13
  • 22
7
#include <iostream>
#include <limits.h> or <climits>

worked for me

Rahul Dutta
  • 71
  • 1
  • 2
1

I ran into similar issue while using LLVM 3.7.1 c++ compiler. Got the following error while trying to compile gRPC code as part of building some custom library

src/core/lib/iomgr/exec_ctx.h:178:12: error: use of undeclared identifier 'INT64_MAX'

The compilation went through after adding -D__STDC_LIMIT_MACROS as one of the options to c++ compiler.

.../bin/c++ -D__STDC_LIMIT_MACROS -I{LIBRARY_PATHS} testlib.cc -o testlib
Ram
  • 11
  • 2
0

Including the following code after #include <iostream> worked for me:

#include <limits.h>

I am using the following compiler:

g++ 5.4.0-6

0

I was also facing the similar problem,just need to add- #include <limits.h> after #include <iostream>

hardik chugh
  • 1,082
  • 15
  • 12
0

The code for all c++ version, compatible with lower GCC like CentOS 6.0(gcc version 4.4.7):

// https://onlinegdb.com/ApNzDNYUx

#include <stdio.h>

// @see https://stackoverflow.com/a/9162072/17679565
#include <inttypes.h>

// For CentOS 6(gcc version 4.4.7) or not defined the macro.
#ifndef INT32_MAX
#define INT32_MAX (2147483647)
#endif

int main()
{
    printf("INT32_MAX=%d\n", INT32_MAX);

    return 0;
}

OnlineGDB

Winlin
  • 1,136
  • 6
  • 25
-1

include --> #include <bits/stdc++.h> <--

should look like :

#include<iostream>
using namespace std;
#include <bits/stdc++.h>
-2
#include<bits/stdc++.h>

add this at the beginning.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103