才开始学习,就是视频上的代码,我输出的却是下面这个样子
#include<stdio.h>#include<math.h>
void main()
{
double x, s;
printf("input number:\n");
scanf("%If", &x);
s = sin(x);
printf("sin of %If is %If\n", x, s);
}
本来应该是
input number:
30
sin of 30 is 。。。。。
但是我的却是
input number:
30
sin of If is If
这是为什么 本帖最后由 jackz007 于 2021-1-18 00:53 编辑
双精度数的格式描述符是 "%lf" (l 是字母 L 的小写),不是 "%If"
#include<stdio.h>
#include<math.h>
int main(void)
{
double x , s ;
printf("input number : ") ;
scanf("%lf" , & x) ; // 按双精度数读入变量 x
s = sin(x * M_PI / 180) ; // M_PI 就是 3.1415926,必须把键入的角度转化为弧度
printf("sin of %.2lf is %.2lf\n" , x , s) ; // "%.2lf" 按 2 位小数输出双精度数
} 常用的输入、输出控制符是必须要弄清楚的:
%d控制输入、输出整型数据
%f控制输入、输出浮点型数据
%c控制输入、输出字符型数据
%lf控制输入、输出双精度型数据(这里是大写字母 L 的小字字母,不是数字 1,也不是大写字母 I)。在输出双精度数据时,可以不写 l,直接用 %f
%u控制输入、输出无符号类型数据
%s控制输入、输出字符串 jackz007 发表于 2021-1-18 00:51
双精度数的格式描述符是 "%lf" (l 是字母 L 的小写),不是 "%If"
你好,但是我发现把M_PI输入后有错误
error C2065: 'M_PI' : undeclared identifier
请问是哪里出错了吗 本帖最后由 jackz007 于 2021-1-20 00:35 编辑
destiny-jhy69 发表于 2021-1-19 23:26
你好,但是我发现把M_PI输入后有错误
error C2065: 'M_PI' : undeclared identifier
请问是哪里出错了 ...
这一条,你加上了吗?
#include<math.h>
math.h 是 C 语言编译器的系统标配,所有的编译器都有,这是 gcc 10.2.0 的 math.h 中的部分片段
#define M_E 2.7182818284590452354
#define M_LOG2E 1.4426950408889634074
#define M_LOG10E 0.43429448190325182765
#define M_LN2 0.69314718055994530942
#define M_LN10 2.30258509299404568402
#define M_PI 3.14159265358979323846 // 这个就是 M_PI 的定义
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.78539816339744830962
#define M_1_PI 0.31830988618379067154
#define M_2_PI 0.63661977236758134308
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2 1.41421356237309504880
#define M_SQRT1_2 0.70710678118654752440
当然,你也可以照原样把 M_PI 的宏定义(就是加了注释的那一行代码)直接复制到自己的源代码文件中。
页:
[1]