Thread: Why a compiler shows a result and other compiler shows other result?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Why a compiler shows a result and other compiler shows other result?

    Hi.
    I have this problem: I used Borland C++ Builder 6 to compile a program and the result was 43380 (witch is correct from my point of view) and I compiled the same .cpp file and I used the same in data with Borland C and it showed -22156.

    I'm asking why is this difference between the result of the compilers because I used the same .cpp file and I entered the same variabiles to get a result?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They're both 0xA974 in hex, the only difference being
    - one is being interpreted as either an unsigned 16 bit integer, or a 32 bit integer,
    - the other is a signed 16 bit integer.

    > I used the same in data with Borland C and it showed -22156.
    Yep, thought so - one's a new 32 bit compiler, and the other is a 16 bit fossil.

    Of course, if your code was better written in that it didn't assume so much about say the range of int, then the problem wouldn't appear
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    I used an unsigned int so it should appear correct.

    I will post the code below to show you the problem:

    Code:
    ...
    the bold instruction is the only instruction witch uses variabiles that can be over the range of int. As you can see I used especialy an unsigned int and the result is still -22156 for an: 84, luna: 11, ziua: 20.
    I don't know why.
    Last edited by RaresLuc; 10-18-2006 at 03:10 PM.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    printf("\n\ncod data: %d",cod);
    printf's %d is for signed integers, use %u instead.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main(void)
    main returns an int.

    > printf("\n\ncod data: %d",cod);
    If you declared it as unsigned int, then you should really use %u to display it.

    Edit: Damn - beaten by the alphabet again
    I'm changing to @@Aardvark
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    uuuu, magic
    That worked. Thanks a lot.

    Quote Originally Posted by Salem
    > void main(void)
    main returns an int.
    I only need the result. I'm not interested yet in what main returns. thx anyway. (I saw you're avatar, hope you're not ........ed ).

    P.S. I supose you saw I'm kind of new in the job but I'm trying hard .

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by RaresLuc
    I only need the result. I'm not interested yet in what main returns. thx anyway.
    It's not a question of what you're interested in. It's a question of what is right according to the C standard, and what just happens to work on your particular compiler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by CornedBee
    It's not a question of what you're interested in. It's a question of what is right according to the C standard, and what just happens to work on your particular compiler.
    I learned that declaration of main in Highschool so till now I thought is correct.
    By the way, why is this not correct if the problem works well and if I don't want to include this program in some other program?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed