DEV Community

Cover image for Input() Syntax in 20 Different Programming Languages
Scofield Idehen
Scofield Idehen

Posted on Originally published at blog.learnhub.africa

Input() Syntax in 20 Different Programming Languages

How to Write Input() in 20 Different Languages 

While working on a project today using C, I realized I had forgotten the input syntax. 

Thanks to ChatGPT, which came in handy, I got the syntax, and that made me smile as the more we use other programming languages, the more we mix and forget some of the core concepts of others. 

As a Python Dev, I have always loved Python's simple yet elegant syntax, and I believe many devs would feel the same. 

I decided to compile 20 different syntaxes for wringing input; if your preferred language is not on the list, you can join the fun and add yours in the comment. 

Python

input("Enter a value: ")
Enter fullscreen mode Exit fullscreen mode

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a value: ");
        String input = scanner.nextLine();
    }
}
Enter fullscreen mode Exit fullscreen mode

C++

#include <iostream>
using namespace std;

int main() {
    cout << "Enter a value: ";
    string input;
    cin >> input;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

const input = prompt("Enter a value:");
Enter fullscreen mode Exit fullscreen mode

Ruby

puts "Enter a value:"
input = gets.chomp
Enter fullscreen mode Exit fullscreen mode

C#

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Enter a value:");
        string input = Console.ReadLine();
    }
}
Enter fullscreen mode Exit fullscreen mode

Swift:

import Foundation

print("Enter a value:")
if let input = readLine() {
    // Use the input here
}
Enter fullscreen mode Exit fullscreen mode

Go:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    fmt.Print("Enter a value: ")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    input := scanner.Text()
}
Enter fullscreen mode Exit fullscreen mode

PHP:

<?php
echo "Enter a value: ";
$input = trim(fgets(STDIN));
?>
Enter fullscreen mode Exit fullscreen mode

Rust:

use std::io;

fn main() {
    println!("Enter a value:");
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
}
Enter fullscreen mode Exit fullscreen mode

MATLAB:

prompt = 'Enter a value: ';
input = input(prompt, 's');
Enter fullscreen mode Exit fullscreen mode

R

input <- readline("Enter a value: ")
Enter fullscreen mode Exit fullscreen mode

Kotlin

import java.util.*

fun main() {
    val scanner = Scanner(System.`in`)
    print("Enter a value: ")
    val input = scanner.nextLine()
}
Enter fullscreen mode Exit fullscreen mode

Perl

print "Enter a value: ";
my $input = <>;
chomp $input;
Enter fullscreen mode Exit fullscreen mode

Lua

io.write("Enter a value: ")
local input = io.read()
Enter fullscreen mode Exit fullscreen mode

Bash

echo "Enter a value: "
read input
Enter fullscreen mode Exit fullscreen mode

PowerShell

$input = Read-Host "Enter a value"
Enter fullscreen mode Exit fullscreen mode

Dart:

import 'dart:io';

void main() {
  stdout.write('Enter a value: ');
  String input = stdin.readLineSync();
}
Enter fullscreen mode Exit fullscreen mode

TypeScript:

const input = prompt("Enter a value:");

Lua (Corona SDK):

local inputField = native.newTextField( display.contentCenterX, display.contentCenterY, 200, 30 )

inputField.placeholder = "Enter a value"
Enter fullscreen mode Exit fullscreen mode

C

include <stdio.h>

int main()
{
  int age;
  printf("How old are you: ");
  scanf("%d", & age);
  printf("You are %d years old", age);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

If you have corrections, please include them and let me know your preferred language. I want the list to get to 50, help me reach my goal. 

Thanks. 

If you find this post exciting, find more exciting posts like this on Learnhub Blog; we write everything tech from Cloud computing to Frontend Dev, Cybersecurity, AI and Blockchain.

Top comments (1)

Collapse
 
yawaramin profile image
Yawar Amin

OCaml:

let input =
  print_endline "Enter a value:";
  read_line ()
Enter fullscreen mode Exit fullscreen mode