errno constant in C++

errno is a preprocessor macro used for error indication.

  • The value of errno is set to zero at program startup, and any function of the standard C++ library are allowed to write positive integers to errno whether or not an error occurred.
  • Once the value of errno is changed from zero to non zero then no other function in the C++ standard library can change its value to zero.The errno is defined in cerrno heder file.
  • The value of errno is set to 33 when there is an error in mathematical argument. In C++ error of mathematical argument is represented by EDOM whose value is 33.

The same header that declares errno () also declares at least the following macro constants with values different from zero:

  • EDOM – Domain error: Some mathematical functions are only defined for certain real values, which is called its domain, for example, the square root and log function is only defined for non-negative numbers so if we pass negative argument in these function they set errno to EDOM
  • ERANGE – Range error: The range of values that can be represented by a variable is limited. For example, mathematical functions such as pow can easily outbound the range representable by a floating point variable, or functions such as strtod can encounter sequences of digits longer than the range representable values. In these cases, errno is set to ERANGE.
  • EILSEQ – Illegal sequence: Multibyte character sequence may have a restricted set of valid sequences. When a set of multibyte characters is translated by functions such as mbrtowc, errno is set to EILSEQ when an invalid sequence is encountered.
  • #include <cerrno>
    #include <clocale>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    using namespace std;
    int main()
    {
        // sqrt function doesn't take negative value
        // thus it changes value of errno to some positive number
        double not_valid = sqrt(-100);
      
        // check if value of errno same as value of EDOM i.e. 33
        if (errno == EDOM) {
            cout << " Value of errno is : " << errno << '\n';
            cout << " -100 is not valid argument for square"
                 << " root function : " << strerror(errno) << '\n';
        }
        return 0;
    }

    out:Value of errno is : 33 -100 is not valid argument for square root function : Numerical argument out of domain

  • 原文地址:https://www.geeksforgeeks.org/errno-constant-in-c/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值