Home > Backend Development > C++ > body text

Explain compile-time initialization and run-time initialization in C programming

王林
Release: 2023-09-03 18:25:04
forward
846 people have browsed it

Explain compile-time initialization and run-time initialization in C programming

让我们用数组的概念来讨论编译时和运行时初始化 -

数组

数组是存储在连续内存位置的项目的集合,元素可以通过使用来访问

编译时数组初始化

在编译时初始化中,用户必须在程序本身中输入详细信息。

编译时初始化与变量相同初始化。数组初始化的一般形式如下 -

语法

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' };
Copy after login

示例

以下是显示数组的 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]);
   }
}
Copy after login

输出

Displaying array of elements :1 2 3 4 5
Copy after login

运行时数组初始化

使用运行时初始化,用户可以在程序的不同运行期间接受或输入不同的值。

它也用于初始化大型数组或具有用户指定值的数组。还可以使用 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&#39;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>");
   }
}
Copy after login

输出

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
Copy after login

The above is the detailed content of Explain compile-time initialization and run-time initialization in C programming. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!