How to get the length of an array in C?

C - Size of an array


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


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



Note: You can use 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.

Print an array element in C using array size:

C Code:

#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;


Size of the array: 6
10 20 30 40 50 60

Contribute your code and comments through Disqus.

Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M