Program to print size of various data types

Program to print size of various data types

  • Write a program to print size of various data types in C
  • Write a program to print size of various data types in C++
  • Write a program to print size of various data types in Python
  • Write a program to print size of various data types in PHP
  • Write a program to print size of various data types in Java
  • Write a program to print size of various data types in Java Script
  • Write a program to print size of various data types in C#

Explanation:

The programming language, compiler, and system architecture (e.g., 32-bit or 64-bit) all affect the size of data types.

Different programming language offers different size ot same data type. For example, in C, integer is of 2 bytes where as its 4 byte in C#.

Program to print size of various data types

#include <stdio.h>

int main() {
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of float: %zu bytes\n", sizeof(float));
    printf("Size of double: %zu bytes\n", sizeof(double));
    printf("Size of char: %zu bytes\n", sizeof(char));
    printf("Size of long: %zu bytes\n", sizeof(long));
    return 0;
}

#include <iostream>

int main() {
    std::cout << "Size of int: " << sizeof(int) << " bytes\n";
    std::cout << "Size of float: " << sizeof(float) << " bytes\n";
    std::cout << "Size of double: " << sizeof(double) << " bytes\n";
    std::cout << "Size of char: " << sizeof(char) << " bytes\n";
    std::cout << "Size of long: " << sizeof(long) << " bytes\n";
    return 0;
}

import sys

print(f"Size of int: {sys.getsizeof(0)} bytes")
print(f"Size of float: {sys.getsizeof(0.0)} bytes")
print(f"Size of str (1 char): {sys.getsizeof('a')} bytes")
print(f"Size of bool: {sys.getsizeof(True)} bytes")
print(f"Size of list (empty): {sys.getsizeof([])} bytes")

<?php
echo "PHP does not have a direct sizeof for data types, but here are examples:\n";
$intVar = 10;
$floatVar = 10.0;
$stringVar = "a";
echo "Size of int: " . strlen(serialize($intVar)) . " bytes\n";
echo "Size of float: " . strlen(serialize($floatVar)) . " bytes\n";
echo "Size of string (1 char): " . strlen($stringVar) . " bytes\n";
?>

public class Main {
    public static void main(String[] args) {
        System.out.println("Size of int: " + Integer.BYTES + " bytes");
        System.out.println("Size of float: " + Float.BYTES + " bytes");
        System.out.println("Size of double: " + Double.BYTES + " bytes");
        System.out.println("Size of char: " + Character.BYTES + " bytes");
        System.out.println("Size of long: " + Long.BYTES + " bytes");
    }
}

console.log("JavaScript does not have fixed sizes for data types as it is dynamically typed.");
console.log("However, approximate sizes in memory are:");
console.log("Size of number (int/float): 8 bytes");
console.log("Size of string (1 char): 2 bytes");
console.log("Size of boolean: 4 bytes");

using System;

class Program {
    static void Main() {
        Console.WriteLine($"Size of int: {sizeof(int)} bytes");
        Console.WriteLine($"Size of float: {sizeof(float)} bytes");
        Console.WriteLine($"Size of double: {sizeof(double)} bytes");
        Console.WriteLine($"Size of char: {sizeof(char)} bytes");
        Console.WriteLine($"Size of long: {sizeof(long)} bytes");
    }
}

List of All Programs