Sizeof Operator in C

Sizeof Operator in C

Sizeof operator in c; Through this tutorial, you will learn everything about sizeof operator in c programming with examples.

Sizeof Operator in C

  • Sizeof Operator
  • Syntax of sizeof() operator
  • Example 1 – Program to Find the Size of Variables
  • Example 2 – C program to print size of variables using sizeof() operator

Sizeof Operator

The Sizeof operator is used to compute the size of its operand. And it returns an integer i.e. total bytes needed in memory to represent the type or value or expression.

The sizeof(variable)operator computes the size of a variable. And, to print the result returned by sizeof, we use either %lu or %zu format specifier.

Syntax of sizeof() operator

The sizeof() operator can be used in various ways; as shown below:

sizeof(type)
sizeof(variable-name)
sizeof(expression)

Example 1 – Program to Find the Size of Variables

#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

Example 2 – C program to print size of variables using sizeof() operator

/*C program to print size of variables using sizeof() operator.*/
 
#include <stdio.h>
 
int main()
{
     
    char    a       ='A';           
    int     b       =120;
    float   c       =123.0f;
    double  d       =1222.90;
    char    str[]   ="Hello";
 
    printf("\nSize of a: %d",sizeof(a));
    printf("\nSize of b: %d",sizeof(b));
    printf("\nSize of c: %d",sizeof(c));
    printf("\nSize of d: %d",sizeof(d));
    printf("\nSize of str: %d",sizeof(str));
 
    return 0;
}

Output

    Size of a: 1
    Size of b: 4
    Size of c: 4
    Size of d: 8
    Size of str: 6

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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