C++ in Visual Studio 2015

Sign in to queue

The Discussion

  • User profile image
    carlospined​ag

    Is the same semantic of pre-increment and post-increment operators in C++ and C#?

    The following C++ program displays 6, either if you compile including the "/clr" option or without this option:

    #include <stdio.h>
    void main()
    {
      int i = 1;
      printf("%i\n",++i + ++i);
    }

    But the following code in C# displays 5:

    using System;
    class P1
    {
      public static void Main()
      {
        int i = 1;
        Console.WriteLine(++i + ++i);
      }
    }

    Also Java displays 5:

    class P1
    {
      public static void main(String[] args)
      {
        int i = 1;
        System.out.println(++i + ++i);
      }
    }

     

  • User profile image
    chrisaverage

    @carlospinedag Order of modifying the same variable in a single sequence point is not specified in c++ and leads to undefined behavior. It can print 5 with one compiler, 6 with another and format your hard drive with some other.

  • User profile image
    sb216

    If the variable i is declared either as static or volatile the output is 5. Just wished to confirm if C++ has any specifications for modification of static or volatile variables in a single sequence point 

  • User profile image
    carlospined​ag

    @chrisaverage: thanks a lot.

Add Your 2 Cents