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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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