Program to demnostrate various data types

Program to demnostrate various data types

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

Explanation:

In programming, a data type is a categorization that defines the type of value that can be stored in a variable. It ascertains:

  • The data type (e.g., character, string, integer, floating-point number, etc.).
  • the actions that can be taken with the information.
  • The memory use of the variable.

What Makes Data Types Important?

  • Memory management: Establishes how much storage space is required for variables.
  • Operations: Limits and specifies what can be done (e.g., concatenation for strings, addition for integers).
  • Type safety stops errors or unexpected operations (like adding a string to an integer).
  • Clarity: Makes code easier to debug and more comprehensible.

Types of data types

Primitive/Basic Data Types:

These are the fundamental types provided by the programming language.

TypeDescriptionExamples
IntegerWhole numbers (positive or negative)int, long, short
Floating-PointNumbers with decimal pointsfloat, double
CharacterA single character (e.g., ‘A’, ‘1’)char
BooleanRepresents true/false valuesbool
VoidRepresents no value (used for functions)void

Composite Data Types
TypeDescriptionExamples
ArrayA collection of elements of the same typeint[], float[], char[]
StringA sequence of characters“Hello CodeCrucks”
StructureA collection of variables of different typesstruct in C
UnionA memory-efficient structureunion in C

Abstract or User-Defined Data Types:

TypeDescriptionExamples
Class/ObjectsUsed in Object-Oriented Programmingclass in Java, C++e
EnumRepresents named integral constantsenum in c, java
Typedef/AliasCustom type definitionstypedef in C

Program to print size of various data types

#include <stdio.h>
#include <stdbool.h>

int main() {
    int intVar = 10;
    float floatVar = 3.14;
    char charVar = 'A';
    char strVar[] = "Hello, World!";
    bool boolVar = true;

    printf("Integer: %d\n", intVar);
    printf("Float: %.2f\n", floatVar);
    printf("Character: %c\n", charVar);
    printf("String: %s\n", strVar);
    printf("Boolean: %s\n", boolVar ? "true" : "false");

    return 0;
}

#include <iostream>
#include <string>

int main() {
    int intVar = 10;
    float floatVar = 3.14f;
    char charVar = 'A';
    std::string strVar = "Hello, World!";
    bool boolVar = true;

    std::cout << "Integer: " << intVar << std::endl;
    std::cout << "Float: " << floatVar << std::endl;
    std::cout << "Character: " << charVar << std::endl;
    std::cout << "String: " << strVar << std::endl;
    std::cout << "Boolean: " << (boolVar ? "true" : "false") << std::endl;

    return 0;
}

int_var = 10
float_var = 3.14
char_var = 'A'
str_var = "Hello, World!"
bool_var = True

print(f"Integer: {int_var}")
print(f"Float: {float_var}")
print(f"Character: {char_var}")
print(f"String: {str_var}")
print(f"Boolean: {bool_var}")

<?php
$intVar = 10;
$floatVar = 3.14;
$charVar = 'A';
$strVar = "Hello, World!";
$boolVar = true;

echo "Integer: $intVar\n";
echo "Float: $floatVar\n";
echo "Character: $charVar\n";
echo "String: $strVar\n";
echo "Boolean: " . ($boolVar ? "true" : "false") . "\n";
?>

public class Main {
    public static void main(String[] args) {
        int intVar = 10;
        float floatVar = 3.14f;
        char charVar = 'A';
        String strVar = "Hello, World!";
        boolean boolVar = true;

        System.out.println("Integer: " + intVar);
        System.out.println("Float: " + floatVar);
        System.out.println("Character: " + charVar);
        System.out.println("String: " + strVar);
        System.out.println("Boolean: " + boolVar);
    }
}

let intVar = 10;
let floatVar = 3.14;
let charVar = 'A';
let strVar = "Hello, World!";
let boolVar = true;

console.log(`Integer: ${intVar}`);
console.log(`Float: ${floatVar}`);
console.log(`Character: ${charVar}`);
console.log(`String: ${strVar}`);
console.log(`Boolean: ${boolVar}`);

class Program {
    static void Main() {
        int intVar = 10;
        float floatVar = 3.14f;
        char charVar = 'A';
        string strVar = "Hello, World!";
        bool boolVar = true;

        Console.WriteLine($"Integer: {intVar}");
        Console.WriteLine($"Float: {floatVar}");
        Console.WriteLine($"Character: {charVar}");
        Console.WriteLine($"String: {strVar}");
        Console.WriteLine($"Boolean: {boolVar}");
    }
}

List of All Programs