C Program to Find Size of Data Types

C Program to Find Size of Data Types

Understanding data types and their sizes is an essential skill for anyone learning C programming. Every variable in C requires memory, and the amount of memory depends on the data type. Knowing the size of data types helps programmers write efficient programs, avoid memory issues, and understand how data is stored in a computer’s memory. Beginners often find this topic fascinating because it provides insight into the inner workings of a computer while also reinforcing fundamental C concepts.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Data types in C, such as int, float, double, and char, have specific sizes that may vary depending on the system architecture. Learning how to determine these sizes using the sizeof operator is crucial for writing portable and reliable programs. In this article, we’ll explore beginner-friendly programs that demonstrate how to find the size of various data types in C, helping you understand memory allocation and type handling more clearly.

Program 1: Find Size of Predefined Data Types

This program demonstrates how to use the sizeof operator to find the size of common C data types on your system. It’s a simple way for beginners to learn about memory allocation.

#include <stdio.h>

int main() {

    printf("Size of char: %zu byte(s)\n", sizeof(char));
    printf("Size of int: %zu byte(s)\n", sizeof(int));
    printf("Size of float: %zu byte(s)\n", sizeof(float));
    printf("Size of double: %zu byte(s)\n", sizeof(double));
    printf("Size of long: %zu byte(s)\n", sizeof(long));
    printf("Size of long long: %zu byte(s)\n", sizeof(long long));

    return 0;

}

In this program, the sizeof operator is used to determine the size of each data type in bytes. The %zu format specifier is used to print the result, which is appropriate for the size_t type returned by sizeof. Beginners can see how memory differs for each data type, which helps in understanding how variables occupy space in memory and why choosing the correct type matters.

Program 2: Find Size Using User-Defined Variables

To make the program more interactive, we can find the size of user-defined variables instead of just predefined data types.

#include <stdio.h>

int main() {

    char c;
    int i;
    float f;
    double d;

    printf("Enter a char: \n");
    scanf(" %c", &c);

    printf("Enter an int: \n");
    scanf("%d", &i);

    printf("Enter a float: \n");
    scanf("%f", &f);

    printf("Enter a double: \n");
    scanf("%lf", &d);

    printf("Size of variable c: %zu byte(s)\n", sizeof(c));
    printf("Size of variable i: %zu byte(s)\n", sizeof(i));
    printf("Size of variable f: %zu byte(s)\n", sizeof(f));
    printf("Size of variable d: %zu byte(s)\n", sizeof(d));

    return 0;

}

In this version, the program asks the user to input values for variables of different types. While the actual value doesn’t change the size, it helps beginners understand that sizeof is applied to variables as well as types. This program reinforces the concept of memory allocation and shows how to use sizeof dynamically in a program.

Program 3: Find Size of Array Elements

You can also find the size of array elements and the total array size. This program demonstrates both concepts.

#include <stdio.h>

int main() {

    int arr[10];

    printf("Size of an int: %zu byte(s)\n", sizeof(int));
    printf("Size of the array arr: %zu byte(s)\n", sizeof(arr));
    printf("Number of elements in arr: %zu\n", sizeof(arr) / sizeof(int));

    return 0;

}

Here, sizeof(arr) gives the total size of the array in bytes, while sizeof(int) gives the size of a single element. Dividing these values gives the number of elements in the array. Beginners can see how memory is allocated for arrays and understand the relationship between array size, element size, and total memory usage. This is particularly useful when working with large arrays or optimizing memory usage.

Program 4: Find Size of a Structure

Structures allow grouping different types of variables under a single name. This program demonstrates how to find the size of a structure in C.

#include <stdio.h>

struct Person {
    char name[50];
    int age;
    float height;
};

int main() {

    struct Person p;

    printf("Size of structure Person: %zu byte(s)\n", sizeof(p));

    return 0;

}

In this program, we define a structure Person containing a char array, an int, and a float. The sizeof operator tells us how much memory the entire structure occupies. Beginners can understand that structures pack multiple variables together, and memory alignment may add padding, affecting total size.

Program 5: Find Size of a Union

Unions are similar to structures but all members share the same memory location. This program shows how sizeof behaves for a union.

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {

    union Data d;

    printf("Size of union Data: %zu byte(s)\n", sizeof(d));

    return 0;

}

Here, the union Data has an int, a float, and a char array. Unlike structures, a union’s size is equal to its largest member. Beginners can see how unions are memory-efficient when only one member is used at a time, which is useful in embedded systems or low-memory programs.

Program 6: Find Size of an Enum

Enums allow defining named integer constants. This program shows how to check the size of an enum.

#include <stdio.h>

enum Colors { RED, GREEN, BLUE, YELLOW };

int main() {

    enum Colors color;

    printf("Size of enum Colors: %zu byte(s)\n", sizeof(color));

    return 0;

}

In this example, the enum Colors assigns integer constants to color names. The sizeof operator reveals that enums typically occupy the same memory as an int. Beginners can understand that enums provide readable names for integer values and help make code easier to maintain.

Frequently Asked Questions (FAQ)

Here are some common questions beginners have about finding the size of data types in C.

Q1. What is the sizeof operator?
The sizeof operator returns the size of a data type or variable in bytes.

Q2. Does the size of data types vary on different systems?
Yes, it can vary depending on the compiler and system architecture (e.g., 32-bit vs. 64-bit systems).

Q3. Can sizeof be used with arrays?
Yes, it can find both the total size of the array and the size of individual elements.

Q4. What format specifier should I use with sizeof?
Use %zu to print the size returned by sizeof, as it returns a size_t type.

Q5. Does the value of a variable affect its size?
No, the size of a variable is determined by its data type, not its value.

Conclusion

Finding the size of data types in C is a beginner-friendly way to understand memory allocation, variable storage, and type handling. By using the sizeof operator with predefined types, user-defined variables, and arrays, beginners gain practical insights into how memory is used in a program. Understanding these concepts is fundamental for writing efficient and reliable programs. Practicing these programs will strengthen your knowledge of data types and prepare you for more advanced topics like pointers, structures, and dynamic memory allocation.

Additional & References

For beginners wanting to explore more about data types, memory, and C programming concepts, the following resources are very helpful:

  1. C Standard Library (stddef.h) – Documentation for size_t and sizeof, essential for understanding memory sizes.
  2. Programiz C Tutorials – Beginner-friendly tutorials with hands-on examples for practicing arithmetic and geometry programs.
  3. GeeksforGeeks C Programming Section – Detailed explanations, sample programs, and exercises for reinforcing core concepts.
  4. Learn-C.org – Interactive platform to write and test C programs online, ideal for experimenting with volume calculations.
Scroll to Top