C Program To Find The Size Of Int, Float, Double And Char

C Program To Find The Size Of Int, Float, Double And Char

In our blog post, “C Program to Find the Size of int float double and char,” we take an engaging stroll through the fundamentals of understanding how a computer allocates memory for various data types. Consider learning about the inner workings of these fundamental concepts in a language that everyone can understand.

This insightful article simplifies complex concepts, allowing you to easily comprehend the fundamentals of size int, float, double, and char variables. Prepare for an exciting experience in the world of programming!

What is sizeof() in C?

In C, the sizeof() operator is used to determine the size, in bytes, of a data type or an expression.

Size of Data Types:

In C, you can use the sizeof operator to determine the size (in bytes) of a data type or a variable. For example:

#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));

    return 0;
}

The sizeof operator returns the size of the specified data type in bytes. The %zu is a format specifier used with printf()

Size of an Array:

You can also use sizeof() to find the size of an array. For example: to print the result, and it’s expecting an argument of type size_t, which is the type returned by sizeof()

#include <stdio.h>

int main() {
    int arr[5];
    printf("Size of arr: %zu bytes\n", sizeof(arr));

    return 0;
}

in this example, sizeof(arr) returns the total size of the array arr in bytes.

Algorithm

Start

  Set the variables to hold the size information for int, float, double, and char..

  Use the sizeof() operator to determine the byte size of each data type.

  Display/Print the sizes of int, float, double, and char.

End

Pseudocode

Declare intSize, floatSize, doubleSize, charSize as integers

intSize = sizeof(int)

floatSize = sizeof(float)

doubleSize = sizeof(double)

charSize = sizeof(char)

Display "Size of int: ", intSize

Display "Size of float: ", floatSize

Display "Size of double: ", doubleSize

Display "Size of char: ", charSize

C Program To Find The Size Of Int, Float, Double And Char

#include <stdio.h>

int main() {

    // Declare variables to store the size of int, float, double, and char

    int intSize;

    float floatSize;

    double doubleSize;

    char charSize;

    // To determine each data type's size, use the sizeof operator.

    intSize = sizeof(int);

    floatSize = sizeof(float);

    doubleSize = sizeof(double);

    charSize = sizeof(char);

    // Output/Print the sizes of int, float, double, and char

    printf("Size of int: %d bytes\n", intSize);

    printf("Size of float: %d bytes\n", floatSize);

    printf("Size of double: %d bytes\n", doubleSize);

    printf("Size of char (character): %d bytes\n", charSize);

    return 0;

}

Output/Complexity Analysis

Size of int: 4 bytes

Size of float: 4 bytes

Size of double: 8 bytes

Size of char: 1 bytes

The program calculates and displays the size of int, float, double, and char in bytes. The output provides the information about the memory occupied by each data type.

How Does This Program Work?

Include Header File:

#include <stdio.h>

This line includes the standard input-output header file, empowering the program to employ essential functions like printf.

This functionality is crucial for presenting information, ensuring seamless communication between the program and the user through clear and formatted output

Declare Variables:

int intSize;

float floatSize;

double doubleSize;

char charSize;

These variables are used to store the sizes of int, float, double, and char respectively.

Calculate Sizes:

intSize = sizeof(int);

floatSize = sizeof(float);

doubleSize = sizeof(double);

charSize = sizeof(char);

We utilize the sizeof() operator to determine the byte size of each data type, and subsequently, these sizes are assigned to their corresponding variables. This process enables us to understand the amount of memory occupied by different data types

Print Results:

printf("Size of int: %d bytes\n", intSize);

printf("Size of float: %d bytes\n", floatSize);

printf("Size of double: %d bytes\n", doubleSize);

printf("Size of char (character): %d bytes\n", charSize);

Here we used printf function to display the sizes of int, float, double, and char on the console.

Return Statement:

return 0;

The main function returns 0 to indicate successful execution to the operating system.

End of Program:

The program ends, and the control returns to the operating system.

Conclusion

This C program efficiently determines and outputs the sizes of int, float, double, and char data types. Understanding the memory requirements of different data types is crucial for efficient memory management in programming. Getting this information is made easier by using the sizeof() operator.


Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *