C array length example

本文介绍了一种在C语言中确定数组长度的方法:通过使用sizeof运算符。文章详细解释了如何利用sizeof运算符来获取数组所占的字节数,并通过除以单个元素的大小来得到数组的长度。

C array length example

 

An array is a contiguous group of data items with the same name and data type. In order to get the length of an array, we have used the sizeof operator.

C Array length example

  
  

In this section you will learn how to determine the length of
an array in C.

An array is a contiguous group of data items with the same name and data
type. In order to get the length of an array, we have used the sizeof operator. The sizeof operator gives
the amount of storage, in bytes, required to store an object. This operator
avoids machine-dependent data sizes in the programs.



#include
-
Coded statements that contain (#)sharp sign are are the
directives of the preprocessor. In our example #include<stdio.h> is
an indication for preprocessor of the compiler and it means to include the stdio.h
header file. 

<stdio.h>- This is the header file
due to which standard input/output functions are included to be performed into
the program. It is necessary to
include this header file as it is having some inbuilt functions like printf(),
scanf()
etc. The angle brackets are used to indicate system include
files.



<conio.h>
- Its a non standard header file that contains clrscr(),
getch()
functions.

void main() - main is the function,
inside which the compiler looks for the data to compile first. Though this
method has default return int but the return type can be set to void
that means now method return type is void and it will return no value.

clrscr()- It is used to clear the console  screen.



sizeof(array)/sizeof(int)
- These are the operators that determines the
length of the array passed inside the operator parentheses..

getch()- This function reads each
character. It holds the console screen and program output on it. Without this
output on the screen will generate in flash and vanishes.

Here is the code:



ARRAYLEN.C




 


#include <stdio.h>

#include <conio.h>

int array[6]12345};

void main() {

  clrscr();

  int len=sizeof(array)/sizeof(int);

  printf("Length Of Array=%d", len);

  getch();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值