c - 在 C 中无效的用户输入应该返回哪个 errno?
问题描述
假设我有一个 C 语言程序,我要求用户输入一个介于 1 和 3 之间的数字。如果用户选择了超出范围的数字,哪个errno最适合在我的函数中返回?
解决方案
I wouldn't use errno for a simple user-facing function like that. errno is mostly for system calls that are reporting low-level I/O, process, and other OS errors. It's not designed for things like bad keyboard input, incorrect file contents, or out of range values. When it's used by user libraries it's typically when they are forwarding internal system errors to the caller and don't want to bother creating an opaque error mechanism to wrap errno.
For example, a JSON library or a TLS encryption library might forward errno values from any failed I/O calls they perform. A multiprocessing library might forward errno values from failed fork/clone syscalls.
That said, if you still want to use errno, EINVAL Invalid Argument
is reasonable.
推荐阅读
- python - 当列表理解不起作用时,标准循环起作用
- python - 分层分组箱线图,
- ios - 无法连接转发到 3306 的 docker mysql
- r - 从R中的经度和纬度获取国家和州/省名称的有效方法?
- reactjs - 错误参考错误:找不到变量:r
- c++ - 如何修复在cpp中打印未知符号?
- windows - 如何从 WSL 调用 PowerShell 脚本?
- android - Android:ImageButton drawable的一部分无故改变颜色
- python - Spotify API:如何将不同级别的 JSON 信息提取到一个 datFrame 中
- java - Java Android Firestore 添加喜欢到帖子