我如何使它可以在我的函數中使用#define變量?我需要創建一個調用此代碼的函數的程序。基本上我的函數底部可以改變,但我的主要功能不能改變這種格式,所以不管我寫我的函數,我必須通過函數傳遞變量a和變量SIZE。但目前看來,SIZE實際上並未被視爲一個整型變量。在函數中使用#define
#include <stdio.h>
#define SIZE 9
int i, position, tmp;
void readArray(int a[]);
void printArray(int a[]);
void sortArray(int a[]);
int main(void)
{
int a[SIZE];
readArray(a);
printArray(a);
sortArray(a);
printf("After sorting:\n");
printArray(a);
return 0;
}
//Functions//
void readArray(int a[]){
printf("Please enter %d integers: ", SIZE);
for (i=0; i<SIZE; i++) {
scanf("%d", &a[i]);
}
}
void printArray(int a[]){
for (i=0;i<SIZE;i++) {
printf("a[%d] = %3d\n", i, a[i]);
}
}
void sortArray(int a[]){
for (i=0; i<SIZE; i++) {
// In each iteration, the i-th largest number becomes the i-th array element.
// Find the largest number in the unsorted portion of the array and
// swap it with the number in the i-th place.
for (position=i; position<SIZE; position++) {
if (a[i] < a[position]) {
tmp = a[i];
a[i] = a[position];
a[position] = tmp;
}
}
}
}
您可能會喜歡閱讀關於特定宏和關於C預處理器的一般信息。 – alk
還縮進您的代碼有助於保持概覽。 – alk