Thread: How do I get rid of "Warning : Constant is long in function main"

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    21

    How do I get rid of "Warning : Constant is long in function main"

    I am trying to get rid of the 117 warnings in the code I was given to modify. The warning I am working on now is shown by”
    Code:
    #include <stdio.h>
    int main()
    {
                    long fpos;
                    fpos= 1000001;
                    if(fpos > 1000000)
                    {
                                    printf("fpos is bigger\n");
                    }
                    else
                    {
                                    printf("Smaller\n");
                    }
                    return(0);
    }
    When I build this code in the Borland C++ version 4.52 IDE configured for the 16 bit 80186 instruction set I get the following messages:

    “Compiling TEST.CPP:
    Warning TEST.CPP 8: Constant is long in function main()
    Warning TEST.CPP 9: Constant is long in function main()
    Linking test.exe:”

    The program is doing what I expect but I want to get rid of the warning.

    The help file says “Inline assembler numeric constants must be integers between -2,147,483,648 and 4,294,967,295, and they must start with one of the digits 0 through 9 or a $ character.”

    So, how can I get rid of the warning?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I compiled it with gcc and -Wall and get nothing.
    Mind that we use to write return 0; and not return (0); ,but this is not a problem.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of 1000001 and 1000000, write 1000001L and 1000000L.

    By the way, do you intend this to be C++? After all, you named your source file TEST.CPP instead of TEST.C
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > When I build this code in the Borland C++ version 4.52 IDE configured for the 16 bit 80186 instruction set I get the following messages:
    Telling us that your ints are 16-bit, which gives you signed ints in the range +/-32K or unsigned up to 64K

    fpos= 1000001;
    if(fpos > 1000000)

    So numeric constants need to be explicitly marked as long, by using the appropriate suffix.
    fpos= 1000001L;
    if(fpos > 1000000L)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Can you even use numbers that big on a 16bit machine?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by homer_3 View Post
    Can you even use numbers that big on a 16bit machine?
    You have a point C data types - Wikipedia, the free encyclopedia

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sure. It's up to the compiler to generate appropriate code to handle 32-bit numbers or more... It may take a few machine instructions to do it. We were able to do 32-bit integers on 8-bit micro processors no problem.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by homer_3
    Can you even use numbers that big on a 16bit machine?
    Yes, on a standard conforming compiler, since long is guaranteed to have a range that those values lie within. I wouldn't dare vouch that Borland C++ version 4.52 is reasonably standard conformant, but given that the compiler gave a warning (that explicitly mentioned long) rather than an error, it should support long constants of the standard minimum range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Thank you all for the inputs. I added the L suffix and the warning went away.
    I want C code. The IDE picked the .CPP suffix for the source file name.
    I will pay attention to this in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-18-2011, 11:39 AM
  2. Replies: 14
    Last Post: 11-08-2010, 01:47 AM
  3. Replies: 10
    Last Post: 08-18-2009, 10:21 AM
  4. "warning: multi-line character constant"
    By Benzakhar in forum C++ Programming
    Replies: 7
    Last Post: 08-24-2008, 03:29 PM
  5. Replies: 8
    Last Post: 03-10-2008, 11:57 AM