banner



How To Get The Size Of An Array In C

Get the Size of Array in C

  1. sizeof() Operator to Determine the Size of an Array in C
  2. Get Length of Array in C

This tutorial introduces how to determine the length of an array in C. The sizeof() operator is used to become the size/length of an array.

sizeof() Operator to Determine the Size of an Array in C

The sizeof() operator is a compile-time unary operator. It is used to calculate the size of its operand. Information technology returns the size of a variable. The sizeof() operator gives the size in the unit of measurement of byte.

The sizeof() operator is used for whatever data blazon such every bit primitives like int, float, char, and also non-primitives data blazon as an array, struct. Information technology returns the memory allocated to that data type.

The output may be different on different machines like a 32-flake organization can show different output and a 64-bit system tin show different of the aforementioned information types.

Syntax of sizeof():

              sizeof(operand)                          
  • The operand is a information-type or any operand.

sizeof() Operator for Archaic Data Types in C

This program uses an int, float as a primitive data type.

              #include <stdio.h>   int chief(void)  {      printf("Size of char data type: %u\n",sizeof(char));      printf("Size of int data blazon: %u\northward",sizeof(int));      printf("Size of float data type: %u\north",sizeof(float));      printf("Size of double data type: %u\north",sizeof(double));           render 0;  }                          

Output:

              Size of char data type: 1 Size of int data blazon: 4 Size of bladder data blazon: 4 Size of double data type: 8                          

Go Length of Array in C

If we divide the assortment's total size by the size of the array chemical element, we get the number of elements in the assortment. The program is as below:

              #include<stdio.h>  int principal(void)  {     int number[16];          size_t n = sizeof(number)/sizeof(number[0]);     printf("Total elements the array can hold is: %d\n",north);          return 0; }                          

Output:

              Total elements the array can hold is: 16                          

When an array is passed as a parameter to the role, it treats equally a pointer. The sizeof() operator returns the pointer size instead of assortment size. So inside functions, this method won't work. Instead, pass an additional parameter size_t size to indicate the number of elements in the array.

              #include<stdio.h> #include<stdlib.h>  void printSizeOfIntArray(int intArray[]); void printLengthIntArray(int intArray[]);  int primary(int argc, char* argv[]) {     int integerArray[] = { 0, 1, 2, 3, four, 5, 6 };      printf("sizeof of the array is: %d\due north", (int) sizeof(integerArray));     printSizeOfIntArray(integerArray);          printf("Length of the array is: %d\n", (int)( sizeof(integerArray) / sizeof(integerArray[0]) ));     printLengthIntArray(integerArray);  }  void printSizeOfIntArray(int intArray[]) {     printf("sizeof of the parameter is: %d\n", (int) sizeof(intArray)); }  void printLengthIntArray(int intArray[]) {     printf("Length of the parameter is: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) )); }                          

Output (in a 64-bit Linux Os):

              sizeof of the assortment is: 28 sizeof of the parameter is: 8 Length of the assortment is: 7 Length of the parameter is: 2                          

Output (in a 32-flake Windows Os):

              sizeof of the array is: 28 sizeof of the parameter is: iv Length of the array is: seven Length of the parameter is: i                          

Write for u.s.

DelftStack manufactures are written by software geeks like you. If you also would like to contribute to DelftStack past writing paid articles, yous can check the write for u.s. page.

Related Commodity - C Array

  • Copy Char Assortment in C
  • Dynamically Allocate an Array in C
  • Clear Char Array in C
  • Assortment of Strings in C
  • Ezoic

    How To Get The Size Of An Array In C,

    Source: https://www.delftstack.com/howto/c/c-length-of-array/

    Posted by: whitmannosty1997.blogspot.com

    0 Response to "How To Get The Size Of An Array In C"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel