C program to measure array size dynamically

Size of Array in C


int size = sizeof(arr) / sizeof(arr[0]);


#include <stdio.h>
int main()
{
 int arr[] = { 10, 20, 30, 40, 50, 60 };
 int arrsize = (arr, sizeof arr / sizeof *arr);
 printf("Number of elements in arr[]:  %d", size_arrsize);
}


#include <stddef.h>
#include <stdio.h>

static const int values[] = { 10, 20, 30, 40, 50, 60 };
#define ARRAYSIZE(x) (sizeof x/sizeof x[0])
int main (int argc, char *argv[])
{
   size_t i;
   printf("Size of the array: %d\n",ARRAYSIZE(values));
   for (i = 0; i < ARRAYSIZE(values); i++)
   {
       printf("%d ", values[i]);
   }
   return 0;
}


The sizeof operator to obtain the size (in bytes) of the data type of its operand. The operand may be an actual type specifier (such as int or float), as well as any valid expression. When the operand is a type name, it must be enclosed in parentheses.
highlightjs.org w3resource.com

Comments

Popular Posts