运算符sizeof_C程序通过使用sizeof()运算符对数组元素进行计数
運(yùn)算符sizeof
sizeof() operator returns the total number of size occupied by a variable, since array is also a variable, we can get the occupied size of array elements.
sizeof()運(yùn)算符返回變量占用的大小總數(shù),由于array也是變量,我們可以獲取數(shù)組元素占用的大小。
邏輯 (Logic)
Get the occupied size of all array elements and divide it with the size of array type. Let suppose there is an integer array with 5 elements then size of the array will be 5*4=20 and the size of array type will be 4. Divide 20 by the 4 answer will be 5 which is the number of array elements.
獲取所有數(shù)組元素的占用大小,然后將其除以數(shù)組類型的大小。 假設(shè)有一個(gè)包含5個(gè)元素的整數(shù)數(shù)組,那么數(shù)組的大小將為5 * 4 = 20,數(shù)組類型的大小將為4。20除以4的答案將為5,這是數(shù)組元素的數(shù)量。
Let's consider the following program
讓我們考慮以下程序
程序計(jì)算C語言中數(shù)組元素的總數(shù) (Program to count total number of array elements in C)
#include <stdio.h> int main() {int arr[]={10,20,30,40,50};int n;n=sizeof(arr)/sizeof(int);printf("Number of elemenets are: %d\n",n);return 0; }Output
輸出量
Number of elemenets are: 5另一種方法 (Another method)
We can divide the occupied size of all array elements by size of any one array element. Consider the following statement:
我們可以將所有數(shù)組元素的占用大小除以任何一個(gè)數(shù)組元素的大小。 考慮以下語句:
n=sizeof(arr)/sizeof(arr[0]);翻譯自: https://www.includehelp.com/code-snippets/c-program-to-count-array-elements-by-using-sizeof-operator.aspx
運(yùn)算符sizeof
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的运算符sizeof_C程序通过使用sizeof()运算符对数组元素进行计数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: puppeteer执行js_使用Node
- 下一篇: HashMap 中的一个“坑”!