sizeof operator in C

sizeof operator in C is an unary operator which can be used to find the size of the data-type or expression or a variable.

It returns the size of the memory allocated (in Bytes) by the compiler. Lets say, we want to find the size of the memory allocated to the data-type int. We can find it by using sizeof operator as shown in the below code:

#include<stdio.h>
#include<conio.h>
void main()
{
     clrscr();
     int sizeOfInt;
     sizeOfInt = sizeof(int);
     printf("%d Bytes", sizeOfInt);
     getch();
}

Output:

2 Bytes

As you can see, the above code calculated the size of int data type and displayed the result.

You may also like...

4 Responses

  1. zerin says:

    Find Max from Array Data
    Find Min from Array Data
    Sort the elements of Array
    Data insert in an array specific position
    Data delete in an array specific position
    I didnt understand the codes

  2. zerin says:

    while sorting in array why do we need this??
    for(i=0;i<no;i++)
    {
    for(j=i;j??? why do we need this??
    {
    if(arr[i] > arr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }

    • learnconline says:

      Hello Zerin,

      In the above program, the sorting logic is as follows:
      Read the first element of an array, compare it with remaining elements which are present after the first element and swap the elements based on the if condition.

      The above program consist of two for loops,
      1) for(i=0;i<no;i++)
      2) for(j=i;j<no;j++)

      Consider a[2] = {2, 1};
      Lets consider program to sort an array in ascending order.
      First for loop with run for 2 iterations
      Iteration 1 on 1st for loop:
      i = 0
      j = 0
      if(arr[0] > arr[0]) => False, so No Swap
      j = 1
      if(arr[0] > arr[1]) => True, so swap

      After swap, a[2] = {1, 2}

      Iteration 2 on 1st for loop:
      i = 1
      j = 1
      if(arr[1] > arr[1]) => False, so no swap

      Final result: a[2] = {1, 2}

      Just to summarize, second for loop is used so that we can compare the previous array elements with all of the array elements following the previous one and swap accordingly in order to sort it.

Leave a Reply

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

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet