How do I debug "INT_MAX undeclared" [closed]
printf(INT_MAX);
limits.h is included, for some reason it's not working in this particular project. In my testbed, it just works. I have no idea how to approach this problem other than removing every single file in the entire project until it starts working. This would be an inhuman amount of work. How can I find this bug faster? What are common causes of this?
Make sure the include directive is
#include <limits.h>
not
#include "limits.h"
Show us a complete program that exhibits the problem, along with the exact error message you're getting. Copy-and-paste both of them. And if you're calling printf(), don't forget the
#include <stdio.h>
And try compiling and running this program:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("INT_MAX = %d\n", INT_MAX);
return 0;
}
It should work correctly, with no warnings or error messages. If it doesn't, there may be something wrong with your compilation system, or with the way it's installed or configured, or with the way you're using it.
I assume you've searched your own code and build system for instances where INT_MAX
might get undefined.
If possible, find a way to compile just the one source file (.c
), so that your iteration speed goes up. It is enough if your Makefile
(or whatever you use) already compiles only the changed file, instead of everything.
Then, in the problematic file, add this before the first #include
, and after every #include
:
#ifndef INT_MAX
#error INT_MAX not defined at this point
#endif
Then rebuild that module and see where the error happens.
If that fails, add the code snippet above here and there in the code to see where it gets undefined.
If it turns out that your <limits.h>
isn't defining it, there are at least two possibilities:
- your platform does not conform to the C standard, at least in the way you are using it; perhaps you need to use a specific compiler switch to get the C standard?
- your code is including a different
limits.h
than the standard one
Happy hacking.
PS. If you need to go through the process of removing files to find the problem, note that you can use binary search to speed things up: first remove one half of the files, and if the problem persists, you know it's in the remaining files, and if not, in the files you removed, or, rarely, in the interaction between the two sets of files. Then iterate using the appropriate set of files, until you narrow it down to something sufficiently small.
This code will never work because printf
takes a pointer to a format string as its first argument. INT_MAX
is not a pointer to a format string but rather an integer.
What you want is printf("%d", INT_MAX)
.
精彩评论