Skip to main content

Part-2: C++, C#, Java Syntax Differences

· 2 min read

[Part-2: Basic Input/Output] C++, C#, Java Syntax Differences

Basic Input/Output

Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user's input from the keyboard.

1. C++

  • Input    - cin>>
  • Output - cout<<
// Print Greetings Program
// Basic Input/Output
void PrintGreeting(char name[])
{
cout<<"Hello " << name <<endl;
}

int main(int argc, char* argv[])
{
char name[100\];

cout<<"Enter Your Name:"; //cout -> Standard Output
cin>>name; //cin -> Standard Input

PrintGreeting(name);

return 1;
}

/* OUT PUT
Enter Your Name:nagvbt
Hello nagvbt
*/

2. C#

  • Input  - Console.WriteLine()
  • Output - Console.ReadLine()
// Print Greetings Program
// Basic Input/Output
namespace CSharpSample
{
class Program
{
static void PrintGreeting(char[] name)
{
Console.WriteLine("Hello "+ new string(name));
}

static int Main(string[] args)
{
char[] name;

Console.WriteLine("Enter Your Name:"); //Console.WriteLine() -> Standard Output
string sName = Console.ReadLine(); //Console.ReadLine() -> Standard Input
name = sName.ToCharArray();

PrintGreeting(name);

return 1;
}
}
}

/* * OUTPUT * *
Enter Your Name:
Nag VBT
Hello Nag VBT
* * * * * * * */

3. Java

Input - System.out.println()

Output - System.in.read()

import java.io.IOException;

class Program
{
public static void PrintGreeting(char[] name)
{
System.out.println("Hello "+ new String(name));
}

public static void main(String[] args)
{
char[] name;

System.out.print("Enter Your Name:"); //System.out.println -> Standard Output

String sName = "";
int tmp;
try
{
while((tmp = System.in.read ()) != 'n') // System.in.read -> Standard Input
{
char c = (char) tmp;
sName = sName + c;
}

}
catch (IOException e)
{
e.printStackTrace();
}

name = sName.toCharArray();
PrintGreeting(name);
}
}

/* OUTPUT
Enter Your Name:Nag VBT
Hello Nag VBT
*/