
让我们用数组的概念来讨论编译时和运行时初始化 -
数组是存储在连续内存位置的项目的集合,元素可以通过使用来访问
在编译时初始化中,用户必须在程序本身中输入详细信息。
编译时初始化与变量相同初始化。数组初始化的一般形式如下 -
type name[size] = { list_of_values };
//integer array initialization
int rollnumbers[4]={ 2, 5, 6, 7};
//float array initialization
float area[5]={ 23.4, 6.8, 5.5,7.3,2.4 };
//character array initialization
char name[9]={ 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', '\0' };以下是显示数组的 C 程序 -
现场演示
#include<stdio.h>
void main(){
//Declaring array with compile time initialization//
int array[5]={1,2,3,4,5};
//Declaring variables//
int i;
//Printing O/p using for loop//
printf("Displaying array of elements :");
for(i=0;i<5;i++){
printf("%d ",array[i]);
}
}Displaying array of elements :1 2 3 4 5
使用运行时初始化,用户可以在程序的不同运行期间接受或输入不同的值。
它也用于初始化大型数组或具有用户指定值的数组。还可以使用 scanf() 函数在运行时初始化数组。
以下是一个 C 程序,用于使用运行时编译计算数组中所有元素的和与积 -
现场演示
#include<stdio.h>
void main(){
//Declaring the array - run time//
int A[2][3],B[2][3],i,j,sum[i][j],product[i][j];
//Reading elements into the array's A and B using for loop//
printf("Enter elements into the array A: </p><p>");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("A[%d][%d] :",i,j);
scanf("%d",&A[i][j]);
}
printf("</p><p>");
}
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("B[%d][%d] :",i,j);
scanf("%d",&B[i][j]);
}
printf("</p><p>");
}
//Calculating sum and printing output//
printf("Sum array is : </p><p>");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
sum[i][j]=A[i][j]+B[i][j];
printf("%d\t",sum[i][j]);
}
printf("</p><p>");
}
//Calculating product and printing output//
printf("Product array is : </p><p>");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
product[i][j]=A[i][j]*B[i][j];
printf("%d\t",product[i][j]);
}
printf("</p><p>");
}
}Enter elements into the array A: A[0][0] :A[0][1] :A[0][2] : A[1][0] :A[1][1] :A[1][2] : B[0][0] :B[0][1] :B[0][2] : B[1][0] :B[1][1] :B[1][2] : Sum array is : 000 000 Product array is : 000 000
以上就是解释C编程中的编译时初始化和运行时初始化的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号