C 库函数 - strtoul()

C 语言教程 · 2019-02-24 06:56:50

描述

C 库函数 unsigned long int strtoul(const char *str, char **endptr, int base) 把参数 str 所指向的字符串根据给定的 base 转换为一个无符号长整数(类型为 unsigned long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。

声明

下面是 strtoul() 函数的声明。

unsigned long int strtoul(const char *str, char **endptr, int base)

参数

  • str -- 要转换为无符号长整数的字符串。
  • endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。
  • base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。

返回值

该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。

实例

下面的实例演示了 strtoul() 函数的用法。

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char str[30] = "2030300 This is test";
   char *ptr;
   long ret;

   ret = strtoul(str, &ptr, 10);
   printf("数字(无符号长整数)是 %lu\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

数字(无符号长整数)是 2030300
字符串部分是 | This is test|

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

The  C Programming Language

The C Programming Language

Brian W. Kernighan、Dennis M. Ritchie / Prentice Hall / 1988-4-1 / USD 67.00

Presents a complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to ta......一起来看看 《The C Programming Language》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具