banner



How To Find 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 Assortment in C

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

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. It returns the size of a variable. The sizeof() operator gives the size in the unit of byte.

The sizeof() operator is used for any information blazon such as primitives like int, bladder, char, and too not-primitives data type as an array, struct. It returns the retentiveness allocated to that data type.

The output may be different on different machines like a 32-bit system can bear witness different output and a 64-flake system can show different of the same data types.

Syntax of sizeof():

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

sizeof() Operator for Primitive Data Types in C

This program uses an int, bladder every bit a archaic data blazon.

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

Output:

              Size of char data type: ane Size of int information type: 4 Size of float data type: iv Size of double data type: 8                          

Get Length of Array in C

If we dissever the array'due south total size by the size of the assortment element, we go the number of elements in the assortment. The programme is as beneath:

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

Output:

              Total elements the array can concur is: 16                          

When an array is passed equally a parameter to the part, it treats every bit a pointer. The sizeof() operator returns the pointer size instead of array size. So inside functions, this method won't piece of 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 principal(int argc, char* argv[]) {     int integerArray[] = { 0, ane, 2, three, 4, 5, vi };      printf("sizeof of the array is: %d\northward", (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-scrap Linux OS):

              sizeof of the array is: 28 sizeof of the parameter is: 8 Length of the array is: 7 Length of the parameter is: two                          

Output (in a 32-flake Windows Bone):

              sizeof of the array is: 28 sizeof of the parameter is: iv Length of the assortment is: vii Length of the parameter is: 1                          

Write for us

DelftStack articles are written by software geeks like you. If you also would similar to contribute to DelftStack past writing paid articles, y'all can check the write for the states folio.

Related Article - C Array

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

    How To Find The Size Of An Array In C,

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

    Posted by: leefolong.blogspot.com

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

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel