c - 技术面试C题的正确答案
问题描述
我有这个代码:
#include <stdio.h>
const float dAverage = 0;
const int a = 100;
const int b = 101;
int main(void) {
int iProduct = 0;
iProduct = a * b;
dAverage = (a + b) / 2.0;
printf("Product a*b =%d\n", iProduct);
printf("Average of a,b=%.3f\n", dAverage);
return 0;
}
问题是“什么是控制台输出?”
因此,在编译时,我得到“main.c:9:13: error: assignment of read-only variable 'dAverage'”
为什么?
解决方案
当前的答案是错误,因为您正在尝试更改常量。如果您的意思是以下内容:
#include <stdio.h>
float dAverage = 0;
const int a=100;
const int b=101;
int main (void){
int iProduct=0;
iProduct=a*b;
dAverage=(a+b)/2.0;
printf("Product a*b =%d\n",iProduct);
printf("Average of a,b=%.3f\n",dAverage);
return 0;
}
那么输出将是:
Product a*b =10100
Average of a,b=100.500