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: ")
- 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();
}
}
- C++
#include <iostream>
using namespace std;
int main() {
cout << "Enter a value: ";
string input;
cin >> input;
return 0;
}
- JavaScript
const input = prompt("Enter a value:");
- Ruby
puts "Enter a value:"
input = gets.chomp
- C#
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Enter a value:");
string input = Console.ReadLine();
}
}
- Swift:
import Foundation
print("Enter a value:")
if let input = readLine() {
// Use the input here
}
- Go:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
fmt.Print("Enter a value: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
}
- PHP:
<?php
echo "Enter a value: ";
$input = trim(fgets(STDIN));
?>
- 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");
}
- MATLAB:
prompt = 'Enter a value: ';
input = input(prompt, 's');
- R
input <- readline("Enter a value: ")
- Kotlin
import java.util.*
fun main() {
val scanner = Scanner(System.`in`)
print("Enter a value: ")
val input = scanner.nextLine()
}
- Perl
print "Enter a value: ";
my $input = <>;
chomp $input;
- Lua
io.write("Enter a value: ")
local input = io.read()
- Bash
echo "Enter a value: "
read input
- PowerShell
$input = Read-Host "Enter a value"
- Dart:
import 'dart:io';
void main() {
stdout.write('Enter a value: ');
String input = stdin.readLineSync();
}
- 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"
- 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;
}
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.