CHEL IS OTOVSUDY
CHEL IS OTOVSUDY

Reputation: 1

Wrong output on Linux

I have this code

int[] Array = { 0, 1, 2, 3, 4, 5 };
int i;

for (i = 0; i < 6; i++)
    Console.Write(" " + Array[i]);

Console.WriteLine("\n New Array");
Array = new int[] { 99, 10, 100, 18, 78, 23, 163, 9, 87, 49 };

for (i = 0; i < 10; i++)
{
    Console.Write(" " + Array[i]);
}

Console.ReadLine();

I am using Visual Studio Code :

Version: 1.82.0

Electron: 25.8.0

ElectronBuildId: 23503258

Chromium: 114.0.5735.289

Node.js: 18.15.0

V8: 11.4.183.29-electron.0

OS: Linux x64 5.15.0-83-generic

This code runs fine on Windows, but on Linux, instead of

0 1 2 3 4 5 
New Array 
99 10 100 18 78 23 163 9 87 49

I get

0 1 2 3 4 5
New Array

Upvotes: -1

Views: 90

Answers (2)

user22527431
user22527431

Reputation:

When you write data (e.g., text) to stdout in Linux, the system doesn't immediately send that data to the screen. Instead, it collects the data in a temporary storage area known as a buffer.

The purpose of buffering is to reduce the number of system calls needed to write data to the output device. Writing data in smaller, frequent chunks can be less efficient than writing it in larger, less frequent chunks.

Under certain conditions, buffers are automatically flushed, such as when the buffer becomes full, or when a newline character ('\n') is encountered. That is the case with WriteLine.

However, Console.Write doesn't flush the buffer, so a simple way to fix this, should be to add a new line character at the end.

Upvotes: 1

Gavr
Gavr

Reputation: 9

in Console.WriteLine("\n New Array"); propably /n is correct

Upvotes: -1

Related Questions