学堂 学堂 学堂公众号手机端

在C语言中,可以使用atoi()函数将字符串转换为整数

lewis 1年前 (2024-03-07) 阅读数 4 #技术

在C语言中,可以使用atoi()函数将字符串转换为整数。示例如下:

#include<stdio.h> #include<stdlib.h> intmain(){ charstr[]="12345"; intnum=atoi(str); printf("Theconvertednumberis:%d\n",num); return0; }

运行结果为:

Theconvertednumberis:12345

此外,还可以使用sscanf()函数来实现字符转数字,示例如下:


#include<stdio.h> intmain(){ charstr[]="67890"; intnum; sscanf(str,"%d",&num); printf("Theconvertednumberis:%d\n",num); return0; }

运行结果为:

Theconvertednumberis:67890
版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门