Variables and types
Demonstrates variable declaration and basic data types: integers, strings, and floats.
C C23
C23
#include <stdio.h>
int main(void) {
int age = 42;
char name[] = "hello"; // C strings are char arrays
double pi = 3.14;
printf("%d\n", age);
printf("%s\n", name);
printf("%g\n", pi);
return 0;
}
Output
42 hello 3.14
C++ C++26
C++26
#include <iostream>
#include <string>
int main() {
int age = 42;
std::string name = "hello";
double pi = 3.14;
std::cout << age << "\n";
std::cout << name << "\n";
std::cout << pi << "\n";
return 0;
}
Output
42 hello 3.14
C# v11.0.100
v11.0.100
var age = 42;
var name = "hello";
var pi = 3.14;
Console.WriteLine(age);
Console.WriteLine(name);
Console.WriteLine(pi);
Output
42 hello 3.14
Go v1.27.4
v1.27.4
package main
import "fmt"
func main() {
var age int = 42
var name string = "hello"
var pi float64 = 3.14
fmt.Println(age)
fmt.Println(name)
fmt.Println(pi)
}
Output
42 hello 3.14
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
int age = 42;
String name = "hello";
double pi = 3.14;
System.out.println(age);
System.out.println(name);
System.out.println(pi);
}
}
Output
42 hello 3.14
JavaScript v26.13.1
v26.13.1
const age = 42;
const name = "hello";
const pi = 3.14;
console.log(age);
console.log(name);
console.log(pi);
Output
42 hello 3.14
PHP v8.6.0
v8.6.0
$age = 42;
$name = "hello";
$pi = 3.14;
echo $age, "\n", $name, "\n", $pi;
Output
42 hello 3.14
Python v3.15.2
v3.15.2
age = 42
name = "hello"
pi = 3.14
print(age)
print(name)
print(pi)
Output
42 hello 3.14
Rust v1.99.1
v1.99.1
fn main() {
let age: i32 = 42;
let name: &str = "hello";
let pi: f64 = 3.14;
println!("{}", age);
println!("{}", name);
println!("{}", pi);
}
Output
42 hello 3.14
TypeScript v26.13.1
v26.13.1
const age: number = 42;
const name: string = "hello";
const pi: number = 3.14;
console.log(age);
console.log(name);
console.log(pi);
Output
42 hello 3.14
Language differences
C: variables must be declared with a type first; strings are char arrays.
Go: statically typed — explicit declarations or compiler inference; supports the := short declaration.
Java: statically typed — explicit declarations or compiler inference.
JavaScript: dynamically typed, no type annotations needed when declaring variables.
PHP: dynamically typed; variables start with $.
Python: dynamically typed, no annotations needed.
Rust: statically typed; let bindings are immutable by default (add mut to make them mutable).