C Program to find the size of int, float, double and char

In this C Programming example, we will implement the program to find the size of int, float, double and char and print the result on the screen.

1. sizeof Operator in C

The First question that arises is, “What is Sizeof() Operator and what does it do?” The answer is “The size of() operator returns the size of the operand and is the most common operator in C. This operator when used along with datatypes, returns the memory allocated of that datatype.

Example: sizeof(a); 
This returns the size in bytes of the variable.

Helpful topics to understand this program better are-


2. C Program to find the size of int, float, double and char

Let’s discuss the execution(kind of pseudocode) for the program to find the size of int, float, double, char datatypes by calculating the memory used by the following datatypes and print it on the console using a sizeof() operator.

  • Initially, the program will compute the result of the datatype the sizeof(datatype)operator.
  • Next, to specify the format we can either use “%lu” or “%zu” format specifiers to print the result on the console.

Let us implement the above concept in the c program to find the size of int, float, double and char.

Note: We can use “%d” to print the size of the datatype and the program will not show any error but the correct way to print the datatype is using “%zu” where z is a length modifier and u stands for an unsigned integer.

#include <stdio.h>
int main() {
  int integerType;
  float floatType;
  double doubleType;
  char characterType;

  // sizeof evaluates the size of a the data type.
  printf("The Size of 'int' data type is : %zu bytes\n", sizeof(integerType));
  printf("The Size of 'float' data type is : %zu bytes\n", sizeof(floatType));
  printf("The Size of 'double' data type is : %zu bytes\n", sizeof(doubleType));
  printf("The Size of 'char' data type is : %zu byte\n", sizeof(characterType));

  return 0;
}
The Size of 'int' data type is : 4 bytes
The Size of 'float' data type is : 4 bytes
The Size of 'double' data type is : 8 bytes
The Size of 'char' data type is : 1 byte

3. Conclusion

In this C Programming example, we have discussed how to find the size of int, float, double and char data types using sizeof operator.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x