日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

运算符sizeof_C程序通过使用sizeof()运算符对数组元素进行计数

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 运算符sizeof_C程序通过使用sizeof()运算符对数组元素进行计数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

運算符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()運算符返回變量占用的大小總數,由于array也是變量,我們可以獲取數組元素占用的大小。

邏輯 (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.

獲取所有數組元素的占用大小,然后將其除以數組類型的大小。 假設有一個包含5個元素的整數數組,那么數組的大小將為5 * 4 = 20,數組類型的大小將為4。20除以4的答案將為5,這是數組元素的數量。

Let's consider the following program

讓我們考慮以下程序

程序計算C語言中數組元素的總數 (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:

我們可以將所有數組元素的占用大小除以任何一個數組元素的大小。 考慮以下語句:

n=sizeof(arr)/sizeof(arr[0]);

翻譯自: https://www.includehelp.com/code-snippets/c-program-to-count-array-elements-by-using-sizeof-operator.aspx

運算符sizeof

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的运算符sizeof_C程序通过使用sizeof()运算符对数组元素进行计数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。