How do I debug "INT_MAX undeclared"

50,258

Solution 1

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.

Solution 2

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.

Solution 3

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).

Share:
50,258

Related videos on Youtube

Blub
Author by

Blub

Updated on March 15, 2020

Comments

  • Blub
    Blub about 4 years

    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?

    • fvu
      fvu almost 13 years
      are you certain you include the right limits.h? There's no other limits.h that it could pick up? If you're using gcc try running it with the -E option which will produce the output of the preprocessor, ie inclusion of header, define substitution etc. That may give a hint about what's going on.
    • R.. GitHub STOP HELPING ICE
      R.. GitHub STOP HELPING ICE almost 13 years
      -1 for fake code and omitting the stuff that's relevant to the error.
  • Blub
    Blub almost 13 years
    that is not the part I'm worried about, I just wrote this to quickly show that the code is simply and the problem is really INT_MAX. The error message is: "INT_MAX undeclared". You would agree that I'd get a different error if my printf usage was the sole problem right?
  • cnicutar
    cnicutar almost 13 years
    @Blub That is not a strong-enough reason to downvote him.
  • Blub
    Blub almost 13 years
    @cnicutar I'm thankful for any help I can get, but that is simply not an answer by any stretch of the meaning. A comment would have been appropiate.
  • Blub
    Blub almost 13 years
    thanks, it turns out that somewhere in the project, there was another limits.h!
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE almost 13 years
    When I see "code" like that, I'm inclined to think that either you have no clue what you're doing, and as such the error message you cite might be from a completely different version - or that you're posting "fake code", which makes it impossible to answer.
  • Blub
    Blub almost 13 years
    @R It's hilarious that you downvote my post now, after it's clear that I downvoted you. You still haven't read the post though, that's why you think I omitted relevant information. Hint: The title caption is important.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE almost 13 years
    I downvoted your post because you provided additional information that you were giving us fake code and no information on which to answer your question. Before that, I though you really just didn't know how to use printf and I tried to provide a reasonable answer. But with the new information, your question is just unanswerable, and as such, it merits a -1.
  • Keith Thompson
    Keith Thompson over 12 years
    @Blub: Then using #include <limits.h> rather than #include "limits.h" would have avoided the problem in the first place. Also, unless there's a really good reason for it, it's probably a bad idea to give a project-specific header the same name as a standard header.
  • Blub
    Blub over 12 years
    I did use the correct include, I have no idea why it still used the project wide header