今天看《c语言入门经典》,照例打打代码练练手.
打了一段算术运算,居然无法运行。
代码如下。问题出现在“scanf()”那一行:
//program 2.8 calculations on a table
include <stdio.h>
int main(void){
float radius = 0.0f;//the raidus of the table
float diameter = 0.0f;//the diameter of the table
float circumference = 0.0f;//the circumference of the table
float area = 0.0f;//the area of the table
float pi = 3.14159265f;
printf("input the diameter of the table:");
scanf("%f", &diameter);//read the diameter from the keyboard
radius = diameter / 2.0f;//calculate the radius
circumference = 2.0f*pi*radius;//calculate the circumference
area = pi*radius*radius;//calculate the area
printf("\nthe circumference is %.2f", circumference);
printf("\nthe area is %.2f\n", area);
return 0;
}
————————————————————————————————————
20170320有新发现:
//program 3.1 a simple example of the if statement
include <stdio.h>
int main(){
int number = 0;
printf("\nenter an integer between 1 and 10: ");
scanf_s("%d", &number);
if (number > 5 )
printf("you entered %d which is greater than 5 \n", number);
if (number < 6)
printf("you entered %d which is less than 6 \n", number);
return 0;
}
问题的关键是把“ scanf ”改为“ scanf_s ”,IDE给出的原因是 scanf unsafe 。
具体为什么,有待解决。
网友评论