Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why there is difference in outputs when i change the compiler?

Why there is difference in outputs when i change the compiler?

Scheduled Pinned Locked Moved C / C++ / MFC
c++comquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nilesh Hamane
    wrote on last edited by
    #1

    CODE: int k = 35; printf( "%d %d %d", k == 35, k = 50, k > 40 ); When i compiled same code in Turbo C++ then its giving output as: 0 50 0 and in Microsoft VC++ its giving output as: 0 50 1 Can anyone tell me why there is difference in outputs when i change the compiler? Thanks Nilesh

    http://nnhamane.googlepages.com/

    C 1 Reply Last reply
    0
    • N Nilesh Hamane

      CODE: int k = 35; printf( "%d %d %d", k == 35, k = 50, k > 40 ); When i compiled same code in Turbo C++ then its giving output as: 0 50 0 and in Microsoft VC++ its giving output as: 0 50 1 Can anyone tell me why there is difference in outputs when i change the compiler? Thanks Nilesh

      http://nnhamane.googlepages.com/

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Different compilers might -and will- interpret the code differently, don't be surprised at this because either some compilers follow standards, some don't, ot they interpret them differently, or the standard itself does not specify every possible aspect of a given problem so the guys making the compiler are free to handle it as they see fit. Based on your code sniplet, i'd say that the Turbo C version will "run" from right to left, so: k > 40 -> 0, since k is at this point 35 which is less than 40 k = 50 -> 50, since k is 50 k == 35 -> 0, sinke k is 50 which is not 35 While the MS compiler is probably doing it in this order: k = 50 -> 50, makes k 50 at first k == 35 -> 0, since k is 50, not 35 k > 40 -> 1 since k is 50 which is greater than 40 There might be other explanations too i guess.

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      N 1 Reply Last reply
      0
      • C Code o mat

        Different compilers might -and will- interpret the code differently, don't be surprised at this because either some compilers follow standards, some don't, ot they interpret them differently, or the standard itself does not specify every possible aspect of a given problem so the guys making the compiler are free to handle it as they see fit. Based on your code sniplet, i'd say that the Turbo C version will "run" from right to left, so: k > 40 -> 0, since k is at this point 35 which is less than 40 k = 50 -> 50, since k is 50 k == 35 -> 0, sinke k is 50 which is not 35 While the MS compiler is probably doing it in this order: k = 50 -> 50, makes k 50 at first k == 35 -> 0, since k is 50, not 35 k > 40 -> 1 since k is 50 which is greater than 40 There might be other explanations too i guess.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

        N Offline
        N Offline
        Nilesh Hamane
        wrote on last edited by
        #3

        Thanks Code-o-mat for your explanation. But it means that there are no standard rules for compilers to interpret the code. Am i correct?

        http://nnhamane.googlepages.com/

        C L 2 Replies Last reply
        0
        • N Nilesh Hamane

          Thanks Code-o-mat for your explanation. But it means that there are no standard rules for compilers to interpret the code. Am i correct?

          http://nnhamane.googlepages.com/

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Well, there!s the ANSI C Standard[^] that i know of, but as said, some things the standard specifies as "undefined", which means -as i said- that different compilers might and will behave differently in case they "meet" something the standard does not clearly have an "oppinion" about. Your best bet to avoid such problems is to try and write code that avoids these and write "obvious" code. For example, depending on what you need you could do:

          ...
          k = 50;
          printf("%d, %d, %d", k == 35, k, k > 40);

          or

          int a = (k == 35);
          k = 50;
          int b = k;
          int c = (k > 40);
          printf("%d, %d, %d", a, b, c);

          and so on...

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

          1 Reply Last reply
          0
          • N Nilesh Hamane

            Thanks Code-o-mat for your explanation. But it means that there are no standard rules for compilers to interpret the code. Am i correct?

            http://nnhamane.googlepages.com/

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Correct. There are holes in the definition of the C language. IIRC the official specification even holds an appendix with issues that the compiler designer has to solve, and has to publish how he did solve them. One of those issues is the order in which side effects may occur, e.g. does i=i++; modify i or not? Your defense against such things is very easy: 1) don't create statements that are ambiguous. In your example, split the line in a couple of lines and use temporary variables, so the parameters get evaluated in the order you choose. 2) or switch to an unambiguous language, such as Java and C# :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups