Call us at 0700-922-6559 or Click here

Write a program to Find the Size of int, float, double and char

#include<stdio.h>

int main() {

    int intType;

    float floatType;

    double doubleType;

    char charType;

    // sizeof evaluates the size of a variable

    printf(“Size of int: %zu bytes\n”, sizeof(intType));

    printf(“Size of float: %zu bytes\n”, sizeof(floatType));

    printf(“Size of double: %zu bytes\n”, sizeof(doubleType));

    printf(“Size of char: %zu byte\n”, sizeof(charType));

    return 0;

}

Output-

Size of int: 4 bytes

Size of float: 4 bytes

Size of double: 8 bytes

Size of char: 1 byte

 let’s break down the C program step by step:

  1. #include <stdio.h>: This line includes the standard input-output library, which provides functions like printf for printing output.
  2. int main(): This line marks the beginning of the main function, which is the entry point of a C program. It returns an integer (int) value to the operating system when the program finishes running. In this case, it takes no arguments, indicated by the empty parentheses ().
  3. {: The curly brace marks the beginning of the main function’s body, where the actual code of the program resides.
  4. int intType;, float floatType;, double doubleType;, char charType;: Here, you declare four variables of different data types: intType (integer), floatType (single-precision floating-point), doubleType (double-precision floating-point), and charType (character).
  5. printf(“Size of int: %zu bytes\n”, sizeof(intType));: This line uses the sizeof operator to determine the size (in bytes) of the intType variable and prints it using printf. The %zu format specifier is used to print the result as an unsigned size_t value.
  6. printf(“Size of float: %zu bytes\n”, sizeof(floatType));: Similarly, this line determines and prints the size of the floatType variable.
  7. printf(“Size of double: %zu bytes\n”, sizeof(doubleType));: This line determines and prints the size of the doubleType variable.
  8. printf(“Size of char: %zu byte\n”, sizeof(charType));: Here, you determine and print the size of the charType variable. Note that it says “byte” instead of “bytes” because char is typically 1 byte in size.
  9. return 0;: This line indicates the end of the main function and returns an exit status of 0 to the operating system, typically indicating successful program execution.
  10. }: The closing curly brace marks the end of the main function’s body and, thus, the end of the program.

When you run this program, it calculates and prints the sizes of the different data types on your system:

Size of int is typically 4 bytes.

Size of float is typically 4 bytes.

Size of a double is typically 8 bytes.

Size of char is typically 1 byte.

These sizes can vary depending on the system and compiler you are using, but the values you provided are common on many systems.

To learn more about C Programming join our course.

Leave a Reply

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