How to get the size of an array in C
This tutorial explains how to determine the length of an array in C. sizeof()
The operator is used to get the size/length of an array.
sizeof()
Operator determines the size of an array in C
sizeof()
The operator is a compile time unary operator. It is used to calculate the size of the operand. It returns the size of the variable. sizeof()
The operator gives the size in bytes.
sizeof()
The operator is used for any data type, primitives like int
, float
, etc. and also for non-primitive data types like , . It returns the memory allocated for that data type .char
array
struct
The output may be different on different machines like a 32-bit system can show different output and a 64-bit system can show different of the same data types.
sizeof()
Syntax
sizeof(operand)
operand
is a data type or any operand.
sizeof()
Operands for primitive data types in C
This program uses int
, float
as primitive data types.
#include <stdio.h>
int main(void)
{
printf("Size of char data type: %u\n",sizeof(char));
printf("Size of int data type: %u\n",sizeof(int));
printf("Size of float data type: %u\n",sizeof(float));
printf("Size of double data type: %u\n",sizeof(double));
return 0;
}
Output:
Size of char data type: 1
Size of int data type: 4
Size of float data type: 4
Size of double data type: 8
Get the length of an array in C
If we divide the total size of the array by the size of the array element, we can get the number of elements in the array. The procedure is as follows.
#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\n",n);
return 0;
}
Output:
Total elements the array can hold is: 16
When an array is passed as a parameter to a function, it is treated as a pointer. sizeof()
The operator returns the pointer size, not the array size. So inside a function, this method does not work. Instead, pass an additional parameter size_t size
indicating the number of elements in the array.
#include<stdio.h>
#include<stdlib.h>
void printSizeOfIntArray(int intArray[]);
void printLengthIntArray(int intArray[]);
int main(int argc, char* argv[])
{
int integerArray[] = { 0, 1, 2, 3, 4, 5, 6 };
printf("sizeof of the array is: %d\n", (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 (on a 64-bit Linux operating system):
sizeof of the array is: 28
sizeof of the parameter is: 8
Length of the array is: 7
Length of the parameter is: 2
Output (on 32-bit Windows operating systems):
sizeof of the array is: 28
sizeof of the parameter is: 4
Length of the array is: 7
Length of the parameter is: 1
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Flushing stdout output stream in C
Publish Date:2025/04/17 Views:200 Category:C语言
-
stdout This article will demonstrate various methods on how to flush an output stream in C language . Use function in C language fflush to refresh stdout output stream I/O The C standard library provides a stdio buffered version of the I/O
Using the feof function in C language
Publish Date:2025/04/17 Views:87 Category:C语言
-
feof This article will introduce several ways to use functions in C language . Use feof the function to check the end-of-file indicator on a file stream in C language feof The function is part of the C standard input/output library and is d
Use the C language setenv function to access environment variables
Publish Date:2025/04/17 Views:122 Category:C语言
-
setenv This article will introduce several methods of using functions to export environment variables in C language . setenv Use function to export environment variables in C language Every program running on a Unix-base system has an envir
在C中将整数转换为字符
Publish Date:2024/01/03 Views:135 Category:C语言
-
本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。
MongoDB 中查询数组大小大于1的文档
Publish Date:2023/05/10 Views:470 Category:MongoDB
-
在处理需要验证数组大小或查找大小大于或小于特定长度的元素的项目时,可能会使用 $size、$where、$exists 等 MongoDB 运算符。下面讨论的方法可以帮助您解决数组长度或数组大小问题。
在 C 语言中使用 typedef enum
Publish Date:2023/05/07 Views:372 Category:C语言
-
本文介绍了如何在 C 语言中使用 typedef enum。使用 enum 在 C 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。
C 语言中的静态变量
Publish Date:2023/05/07 Views:170 Category:C语言
-
本文介绍了如何在 C 语言中使用静态变量。在 C 语言中使用 static 变量在函数调用之间保存变量值
C 语言中生成随机数
Publish Date:2023/05/07 Views:159 Category:C语言
-
本文演示了如何在 C 语言中生成随机数。使用 rand 和 srand 函数在 C 语言中生成随机数
C 语言中的 i++ 与++i
Publish Date:2023/05/07 Views:130 Category:C语言
-
本文演示了如何在 C 语言中使用前缀增量与后缀增量运算符。C 语言中++i 和 i++ 记号的主要区别