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. bzoj 1026 windy数
  2. JS中的this的应用总结
  3. 信息安全技术 网络安全等级保护测评要求_【诚资讯】等保2.0版本出炉!信息安全技术 网络安全等级保护基本要求正式发布...
  4. python怎么放音乐_python怎么播放音乐
  5. @query传参_vue-router中params传参和query传参的区别及处理方法
  6. 惠新宸php教程_百度PHP高级顾问惠新宸:PHP在百度的发展历程
  7. N点虚拟主机管理系统(For Windows2003/2008)功能及介绍
  8. 【AI视野·今日CV 计算机视觉论文速览 第194期】Mon, 10 May 2021
  9. azure多功能成像好用吗_了解Azure持久功能
  10. ASP.NET Web开发框架之八 所有ERP部分的源代码全部开放下载
  11. 自己构造公共程序(3)--DataGrid小方法
  12. 小心投机分子绿坝软件的苦肉计
  13. python中的换行符是哪个键_Python换行符问题:\r\n还是\n?
  14. C盘满了怎么办?最强清理工具来了
  15. mysql 拼接字符串查询
  16. 计算机无法安装蓝牙驱动,win7电脑蓝牙驱动怎么安装,教您怎么安装
  17. Ubuntu安装腾达u12驱动
  18. 分账功能对接解决方案
  19. 怎样调整计算机桌面字体大小,如何把电脑字体调小?教你怎么调整电脑屏幕的字体大小...
  20. IBM developerWorks 技术主题 Linux 文档库

热门文章

  1. JWT—JSON Web Token - 理解JWT网络间应用用户安全认证交互设计
  2. Sqlserver2008 数据库镜像会话的初始连接
  3. [置顶] Android之Handler用法总结
  4. 查询数据库里所有表名,字段名的语句
  5. [转载] [转载] python中的Numpy库入门
  6. [转载] java中抽象类的定义和使用
  7. 【原创】Altium Designer Winter 09 笔记 之一
  8. hdfs居然无法正常停止
  9. redis运行状态图形化监控工具 — RedisLive
  10. Flask 应用最佳实践