zoukankan      html  css  js  c++  java
  • 不要将正常值和错误标志混在一起返回

    不要将正常值和错误标志混在一起返回。

    正常值用输出参数获得 ,而 错误标志用 return 语句返回。

    C标准库函数的设计者为什么要将 getchar声明为令人迷糊的 int 类型呢?

    他会那么傻吗?

    在正常情况下,getchar 的确返回单个字符。

    但如果 getchar 碰到文件结束标志或发 生读错误,它必须返回一个标志 EOF。

    为了区别于正常的字符,只好将 EOF 定义为负数 (通常为负 1)。

    因此函数 getchar 就成了 int 类型。

    我们在实际工作中,经常会碰到上述令人为难的问题。

    为了避免出现误解,我们应 该将正常值和错误标志分开。

    即:正常值用输出参数获得,而错误标志用 return 语句返 回。

    函数 getchar 可以改写成 BOOL GetChar(char *c); 虽然 gechar 比 GetChar 灵活,例如 putchar(getchar()); 但是如果 getchar 用错了, 它的灵活性又有什么用呢?

     1 #include <iostream>
     2 #include <stdlib.h>
     3 #include <math.h>
     4 
     5 //main()函数的定义
     6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     7 using namespace std;
     8 int main(int argc, char** argv) {
     9  double x;
    10 
    11     //循环输入数据计算对数
    12     do {
    13         cout<<"x=";
    14         cin>>x;
    15         if (x<=0) break;
    16         cout<<"log("<<x<<")="<<log(x)<<endl;
    17         cout<<"log10("<<x<<")="<<log10(x)<<endl;
    18     } while(1);
    19 }
  • 相关阅读:
    二叉查找树
    huffman coding
    普通树与二叉树
    递归转循环的通法
    尾递归和JAVA
    编译器和解释器
    开天辟地-用visualstudio2010编写helloworld
    Android app targetSdk升级到27碰到的一个bug补充说明
    Android Studio修改Apk打包生成名称
    Glide3升级到Glide4碰到的问题汇总以及部分代码修改
  • 原文地址:https://www.cnblogs.com/borter/p/9413583.html
Copyright © 2011-2022 走看看