小知识点(2)
1.pow函数
之前在使用pow函数时经常出错,返回值为0(输出一直为0),就找不到出错的原因,直到最近一次使用才发现自己忽略的点。
(1)注意pow
函数的定义
- 原型:在TC2.0中原型为extern float pow(float x, float y); ,而在VC6.0中原型为double pow( double x, double y );
- 头文件:math.h/cmath(C++中)
- 功能:计算x的y次幂。
- 返回值:x不能为负数且y为小数,或者x为0且y小于等于0,返回幂指数的结果。
- 返回类型:double型,int,float会给与警告
(2)示例
直接输出:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
a=b=3;
printf("%f",pow(a,b));
return 0;
}
使用整型的返回值
#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
a=b=3;
int s=pow(a,b);
printf("%d",s);
return 0;
}
这个时候就可以直接用%d
格式输出;
2.puts函数
- puts函数出现乱码:puts函数在正常输出字符串时,遇到字符串末尾的
'\0'
结束符表示字符串已结束,停止输出;若字符串末尾没有'\0'
,就会非法输出内存。 - puts在输出字符串后会自动输出一个回车符。