0

I have been asked this questions before but this time i want to know the difference between the compiler of C++ and C#.

C# coding of array

static void Main(string[] args)
    {
        int n;
        int[] ar = new int[50];
        Console.Write("Enter the size of array= ");
        n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            ar[i] = int.Parse(Console.ReadLine());
        }
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine("AR[" + i + "]=" + ar[i]);
        }
        Console.Read();
    }

Output

C# output

C++ coding of array

int main()  
{  
    clrscr();  
    int x[10];  
    int n;  
    cout << "Enter the array size= ";  
    cin >> n;  
    cout << "Enter the elements for array= ";  
    for(int i = 0; i < n; i++)  
    {  
        cin >> x[i];  
    }
    for(int i = 0; i < n; i++)  
    {  
        cout << "x[" << i << "]=" << x[i] << "\n";  
    }   
    getch();  
    return 0;  
}

Output

C++ output
Now here my question is this when m giving the same input for the C# then its asking for 4 elements to input and leave the 0 before any digit. But when m going with same input in C++ then its considering the 0 with any digit a separate input even m giving it with a digit and took less input then i entered.

Even both languages follow the OOP approach. So what are the difference between both compiler. Why these are taking a lots of different input and generating different output.

One more that bother me that why the C++ compiler not reading 0 for last element and print the 7 but my input is 07 so according to its above output it should be 0 not 7.

4
  • 2
    This is probably due to a buggy implementation of C++ by the Turbo C++ compiler. Time to look up the standard... Commented Jul 10, 2011 at 2:30
  • 3
    Not sure what your compiler is, but when I run your c++ code it produces the same result as the C# one. Commented Jul 10, 2011 at 2:33
  • R you using the BCC or GCC compiler. Its was Borland old compiler. Commented Jul 10, 2011 at 2:43
  • Where is Eric. I think its not sunday for him......;) Commented Jul 10, 2011 at 6:08

2 Answers 2

5

This is a complete guess but the Borland C++ is trying to interpret a number with a leading 0 as octal (which is a standard convention like the '0x' prefix for hexadecimal). Since 08 isn't a valid octal number (only 0-7 are valid digit it octal) it is splitting it up into two inputs.

Try entering '010' and if the program prints out 8 you'll know it is interpreting anything with a leading zero as octal.

You could also trying to force cin to interpret your input as decimal by changing your input line to:

cin >> dec >> x[i];
Sign up to request clarification or add additional context in comments.

2 Comments

Its right regarding the c++ for borland compiler so What about C# why it don't determine the octal no.....And see the comment of @Petar Lvanov
I don't have any better answer than because that's the way it works. C#. The documentation for parsing numeric strings makes no mention of octal and the C# compiler doesn't recognize numbers with a leading 0 as octal, so apparently Microsoft doesn't support octal in .NET. As for C++ I'm sure that there is no standard defining how numbers should be parsed - so each implementation does whatever it wants to.
3

First, it shouldn't be any surprise that using two different functions in two different languages that are meant to do the same thing will give you subtly different results. If you want to know what each of them does, look at the documentation.

More specifically, cin >> i in C++ certainly does not act the same way as int.Parse(Console.ReadLine()) in C#. For example, the C++ version is able to read multiple numbers from the same line (delimited by spaces), while the C# version isn't.

But for your specific example, this looks either like a bug in the compiler you use, or it could be that the specification is unclear. If I compile your C++ code in Visual Studio, it behaves the same way as the C# version.

Also, I'm not sure what exactly did you mean, but your C++ code certainly does not “follow OOP approach”. (I'm not saying it should, just that it doesn't.)

4 Comments

plz explain why C++ code is reading only 3 inputs while C# reading 4 in above examples.
@Javed, like I said, it does not do that for me. And I think it could be a compiler bug in Turbo C++.
I want also know this and agree with the @Javed
The c++ code is reading 4 inputs: 0, 8, 98, and 7. The library used by the code isn't following the standards. 08 is not a valid octal number, so the library sees an invisible barrier between the 0 and the 8. It reads the 0 as one number and the 8 as another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.