c语言中isdigit函数

In this article, we’ll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let’s look at how to use this function, using some simple examples.

在本文中,我们将研究在C / C ++中使用isdigit()函数。 这是检查任何值是否为数字的非常简单的方法。 让我们通过一些简单的示例来看看如何使用此功能。



C / C ++中isdigit()的基本语法 (Basic Syntax of isdigit() in C/C++)

This function takes a characer value val and returns 0 or 1, if the value is a numerical digit.

此函数采用字符值val ,如果该值是数字,则返回0或1。

This is defined in the <ctypes.h> header file, so we must also include this file.

这是在<ctypes.h>头文件中定义的,因此我们还必须包含此文件。


#include <ctypes.h>int isdigit(char value);

The value must be of such a form that it can be converted to an unsigned char, such as a positive integer / ASCII character.

该值必须具有可以转换为unsigned char ,例如正整数/ ASCII字符。

But note that integers will not be considered digits, since only their ascii equivalents are considered.

但请注意,整数将不被视为数字,因为仅考虑其ascii等效项。

在C ++中使用isdigit() (Using isdigit() in C++)

While you can simply copy and paste the C code to C++ too, the C++ standard library already has the implementation of isdigit()!

虽然您也可以简单地将C代码复制并粘贴到C ++,但是C ++标准库已经实现了isdigit()

This make our lives much more simpler, as we can directly use it, from the std namespace:

这可以使我们的生活更加简单,因为我们可以从std名称空间直接使用它:


std::isdigit(char value);

This also returns a bool value instead, but this is equivalent to the integer values 0 and 1, returned by the C version!

它也返回bool值,但它等效于C版本返回的整数值0和1!



在C / C ++中使用isdigit() (Using isdigit() in C/C++)

Let’s now see some examples to check if a value is a digit or not.

现在,让我们看一些示例来检查值是否为数字。

We’ll take up a few integer numbers and some characters too, and apply isdigit() on them.

我们还将使用一些整数和一些字符,并将isdigit()应用于它们。


#include <stdio.h>
#include <ctype.h>int main() {int a = 9;int b = 10;int c = 49;char d = 'a';char e = '0';printf("Is %d a digit?\n", a);if (isdigit(a)) printf("Yes\n"); else printf("No\n");printf("Is %d a digit?\n", b);if (isdigit(b)) printf("Yes\n"); else printf("No\n");printf("Is %d a digit?\n", c);if (isdigit(c)) printf("Yes\n"); else printf("No\n");printf("Is %c a digit?\n", d);if (isdigit(d)) printf("Yes\n"); else printf("No\n");printf("Is %c a digit?\n", e);if (isdigit(e)) printf("Yes\n"); else printf("No\n");return 0;
}

Output

输出量


Is 9 a digit?
No
Is 10 a digit?
No
Is 49 a digit?
Yes
Is a a digit?
No
Is 0 a digit?
Yes

As you can observe, the integers 9 and 10 were not considered digits. But the integer 49 has an ASCII character of ‘0’, and this is a digit!

如您所见,整数9和10不被视为数字。 但是整数49的ASCII字符为“ 0”,这是一个数字!

This is exactly the same as '0', and therefore, this character is also a digit, according to isdigit(char)!

这与'0'完全相同,因此,根据isdigit(char) ,此字符也是一个数字!



结论 (Conclusion)

In this article, we learned about using the isdigit() function in C/C++. This made us easily check if any character is a digit or not!

在本文中,我们学习了在C / C ++中使用isdigit()函数的知识。 这使我们可以轻松检查任何字符是否为数字!

You can look at more tutorials on C programming here. Until next time!

您可以在此处查看有关C编程的更多教程。 直到下一次!

参考资料 (References)

  • Linux manual page on isdigit()isdigit()上的Linux手册页


翻译自: https://www.journaldev.com/39998/isdigit-in-c-plus-plus

c语言中isdigit函数

c语言中isdigit函数_在C / C ++中使用isdigit()相关推荐

  1. c语言中sprintf函数_在C / C ++中使用sprintf()函数

    c语言中sprintf函数 In this article, we'll take a look at using the sprintf() function in C / C++. 在本文中,我们 ...

  2. c语言中memcpy函数_带有示例的C中的memcpy()函数

    c语言中memcpy函数 memcpy()函数 (memcpy() function) memcpy() is a library function, which is declared in the ...

  3. C语言中perm函数的作用,C语言中有关处理系统时间的知识

    <C语言中有关处理系统时间的知识>由会员分享,可在线阅读,更多相关<C语言中有关处理系统时间的知识(9页珍藏版)>请在人人文库网上搜索. 1.C语言中有关处理系统时间的知识C语 ...

  4. C语言中tan函数的参数,C语言中正切的相关函数总结

    C语言tan()函数:正切函数头文件: #include tan() 函数用来求给定值的正切值,其原型为: double tan(double x); [参数]x 为给定的弧度值. [返回值]返回 x ...

  5. c语言中fputc函数的作用是,C语言中fputc函数的用法_后端开发

    Python画ROC曲线和AUC值计算(附代码)_后端开发 ROC(Receiver Operating Characteristic)曲线和AUC常被用来评价一个二值分类器的优劣.这篇文章将先简单的 ...

  6. php seekdir,C++_详解C语言中telldir()函数和seekdir()函数的用法,C语言telldir()函数:取得目录流 - phpStudy...

    详解C语言中telldir()函数和seekdir()函数的用法 C语言telldir()函数:取得目录流的读取位置头文件: #include 定义函数: off_t telldir(DIR *dir ...

  7. r语言中c函数错误,R语言中c()函数与paste()函数的区别说明

    c()函数:将括号中的元素连接起来,并不创建向量 paste()函数:连接括号中的元素 例如 c(1, 2:4),结果为1 2 3 4 paste(1, 2:4),结果为"1 2" ...

  8. r语言中paste函数_R中的paste()函数-简要指南

    r语言中paste函数 Using the paste() function in R will be straight and simple. In this tutorial let's see ...

  9. C语言中malloc函数及free函数用法(定义动态数组及释放)

    一.关于静态数组.动态数组 二.malloc()和free()的基本概念以及基本用法 1.函数定义 2.函数用法 3.关于函数使用注意事项 三.malloc()函数分配内存空间的由来 四.malloc ...

最新文章

  1. LeetCode简单题之只出现一次的数字
  2. 大型网站架构模式之一
  3. 跟我一起写 Makefile(八)
  4. [ASP.NET Core] Static File Middleware
  5. 中科院自动化所:最新视觉-语言预训练综述
  6. linux编写一个简单的端口扫描程序,小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)...
  7. Android学习笔记---08_短信发送器的制作
  8. 指针常量、常量指针和常量指针常量
  9. Hive(五)——查询
  10. 具体数学-第13课(组合数各种性质)
  11. 英语中的完成时态的比较
  12. 01 牛刀小试【PAT A1046】Shortest Distance
  13. Web开发分享qq、微信组件-百度分享
  14. 我的项目day04:首页,轮播图前后端,登录注册功能设计,cgi,uwsgi,多方式登录接口,手机号是否存在接口,腾讯云短信,模态框,腾讯短信功能二次封装,短信验证接口,短信注册接口,断行注册接口
  15. 考研数学1-考试大纲(2022年)
  16. bcdedit添加linux引导,用BCDEdit编辑启动菜单
  17. JAVA基础语法(1)
  18. 使用qq邮箱发送邮件
  19. 20210905 Ax=b的解的三种情况
  20. 四旋翼无人机学习第23节--原理图与PCB库开源计划

热门文章

  1. C#用域账号登陆,访问网络路径
  2. 17.3.10--关于C元的变量类型所占字节问题和类型转化
  3. C# File流操作
  4. 关于Object[]数组强转成Integer[]类型的数组.
  5. 二维平面上判断点是否在三角形内
  6. PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unknow........
  7. Python语法基础:面向对象2
  8. 在win7在结构cocos2d-x v3.2rc0开发环境(For Android)
  9. 2013年工作中用到的10个命令:11-20
  10. 常见HTTP状态(304,200等)转