Memory Size Calculator

Memory Size Calculator

Medium C Variables 36 views
Explanation Complexity

Problem Statement

By printing the size of different datatypes (char, int, float, double, long, pointers). Then create array of each type and verify that array size = element_count * sizeof(type).

Input Format

No user input.
Different C data types and their arrays are created in the program.

Output Format

• Size of each data type

• Size of each array

• Verification that
array size = number of elements × sizeof(type)

Example

Data types used in C:
char, int, float, double, long, pointer
char size = 1
int size = 4
float size = 4
double size = 8
long size = 8
pointer size = 8

char array size = 5
int array size = 20
float array size = 20
double array size = 40

Constraints

• Output depends on system (32-bit / 64-bit)

Concept Explanation

sizeof(type) gives size of one element.
sizeof(array) gives total memory of the array.

Step-by-Step Explanation

1.Print sizeof(char), sizeof(int), sizeof(float), sizeof(double), sizeof(long), sizeof(pointer).

2.Create arrays of size 5 for each type.

3.Print sizeof(array).

4.Verify:

• sizeof(array) = number_of_elements × sizeof(type).

Concept Explanation

sizeof(type) gives size of one element.
sizeof(array) gives total memory of the array.

Step-by-Step Explanation

1.Print sizeof(char), sizeof(int), sizeof(float), sizeof(double), sizeof(long), sizeof(pointer).

2.Create arrays of size 5 for each type.

3.Print sizeof(array).

4.Verify:

• sizeof(array) = number_of_elements × sizeof(type).

Input / Output Format

Input Format
No user input.
Different C data types and their arrays are created in the program.
Output Format
• Size of each data type

• Size of each array

• Verification that
array size = number of elements × sizeof(type)
Constraints
• Output depends on system (32-bit / 64-bit)

Examples

Input:
Data types used in C: char, int, float, double, long, pointer
Output:
char size = 1 int size = 4 float size = 4 double size = 8 long size = 8 pointer size = 8 char array size = 5 int array size = 20 float array size = 20 double array size = 40

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int n = 5;

    /* ---- Size of basic data types ---- */
    printf("Size of char   = %zu bytesn", sizeof(char));
    printf("Size of int    = %zu bytesn", sizeof(int));
    printf("Size of float  = %zu bytesn", sizeof(float));
    printf("Size of double = %zu bytesn", sizeof(double));
    printf("Size of long   = %zu bytesn", sizeof(long));
    printf("Size of pointer= %zu bytesnn", sizeof(int*));

    /* ---- Arrays of each type ---- */
    char cArr[5];
    int iArr[5];
    float fArr[5];
    double dArr[5];
    long lArr[5];
    int *pArr[5];   // array of pointers

    /* ---- Verify array size formula ---- */
    printf("char array size   = %zu (Expected: %zu)n",
           sizeof(cArr), n * sizeof(char));

    printf("int array size    = %zu (Expected: %zu)n",
           sizeof(iArr), n * sizeof(int));

    printf("float array size  = %zu (Expected: %zu)n",
           sizeof(fArr), n * sizeof(float));

    printf("double array size = %zu (Expected: %zu)n",
           sizeof(dArr), n * sizeof(double));

    printf("long array size   = %zu (Expected: %zu)n",
           sizeof(lArr), n * sizeof(long));

    printf("pointer array size= %zu (Expected: %zu)n",
           sizeof(pArr), n * sizeof(int*));

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int n = 5;

    /* ---- Size of basic data types ---- */
    printf("Size of char   = %zu bytesn", sizeof(char));
    printf("Size of int    = %zu bytesn", sizeof(int));
    printf("Size of float  = %zu bytesn", sizeof(float));
    printf("Size of double = %zu bytesn", sizeof(double));
    printf("Size of long   = %zu bytesn", sizeof(long));
    printf("Size of pointer= %zu bytesnn", sizeof(int*));

    /* ---- Arrays of each type ---- */
    char cArr[5];
    int iArr[5];
    float fArr[5];
    double dArr[5];
    long lArr[5];
    int *pArr[5];   // array of pointers

    /* ---- Verify array size formula ---- */
    printf("char array size   = %zu (Expected: %zu)n",
           sizeof(cArr), n * sizeof(char));

    printf("int array size    = %zu (Expected: %zu)n",
           sizeof(iArr), n * sizeof(int));

    printf("float array size  = %zu (Expected: %zu)n",
           sizeof(fArr), n * sizeof(float));

    printf("double array size = %zu (Expected: %zu)n",
           sizeof(dArr), n * sizeof(double));

    printf("long array size   = %zu (Expected: %zu)n",
           sizeof(lArr), n * sizeof(long));

    printf("pointer array size= %zu (Expected: %zu)n",
           sizeof(pArr), n * sizeof(int*));

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.