linux应用编程--简单常用函数
来源:互联网 发布:北斗卫星软件 编辑:程序博客网 时间:2024/04/27 16:47
一、atoi函数
#include <stdlib.h>int atoi(const char *nptr);
功能:将一个数字字符串转换成对应的十进制数
案例一:
#include <stdio.h>int main(int argc,char *argv[]){ printf("atoi(%s) = %d.\n",argv[1],atoi(argv[1])); return 0;}
运行结果:
二、格式化输出函数
#include <stdio.h>int printf(const char *format, ...);int fprintf(FILE *stream. const char *format, ...);int sprintf(char *str, const char *format, ...);我们看最后一个实例
#include <stdio.h>int main(){ int data; char buf[100]; scanf("%d", &data); sprintf(buf, "the data is %d\n", buf); printf("buf : %s\n", buf); return 0;}
运行结果:
三、字符串处理函数
A.字符串拷贝
#include <string.h>char *strcpy(char *dest, const char *src);char *strncpy(char *dest, const char *src, size_t n);B.字符串链接函数
#include <string.h>char *strcat(char *dest, const char *src);char *strncat(char *dest, const char *src, size_t n);C.字符串比较函数
#include <string.h>int strcmp(const char *s1,const char *s2);int strncmp(const char *s1,const char *s2,size_t n);D.字符串分割函数
#include <string.h>char *strtok(char *str, const char *delim);
函数说明:strtok()用来将字符串分割成一个个片段。
参数str指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数str的字符串中发现参数delim的分割字符时则会将该字符该为\0字符。
第一次调用时,strtok()必须给予参数str字符串,往后的调用将参数str设置成NULL,每次调用成功返回下一个分割后的字符串指针。
返回值:返回下一个分割后的字符串指针,如果已无从分割则返回NULL。
案例三:
#include <string.h>#include <stdio.h>int main(){ char s[] = "ab-cd : ef;gh:i-ijkl;mnop;qrs-tu;vwx-y;z"; char *delim = "-;;"; char *p; printf("%s", strtok(s,delim)); while((p = strtok(NULL, delim))) printf("%s", p); printf("\n"); return 0;}
运行结果:
E.复制字符串函数
#include <string.h>char *strdup(const char *s);char *strndup(const char *s, size_t n);
注意:
1、与strcpy不同的是,strcpy函数内部不会分配空间,进行的是直接拷贝
2、strdup函数内部会根据创建来的字符串分配空间,并且进行拷贝。
案例四:
#include <string.h>#include <stdlib.h>int main(){ char *str; char buf[] = "hello world"; str = strdup(buf); printf("str = %s\n", str); free(str); str = strndup(buf, 5); printf("str = %s\n", str); free(str); return 0;}运行结果:
0 0
- linux应用编程--简单常用函数
- Linux - 编程常用函数
- linux编程常用函数
- linux进程编程(一)-- fork()函数简单应用
- Linux网络编程常用函数
- Linux网络编程常用函数
- Linux网络编程常用函数
- Linux网络编程常用函数
- Linux编程常用的函数
- Linux网络编程常用函数
- LINUX编程常用的函数
- Linux编程常用的函数
- linux编程常用函数说明
- Linux编程常用的函数
- linux时间编程常用函数
- linux socket 编程 常用函数
- linux编程常用函数接口
- linux编程常用的函数
- java内部类注意事项
- 在JSP中使用Integer.parseInt()报错的问题
- 十六进制颜色码图片大全
- 表单的method特性
- 【NOIP2016提高A组模拟10.15】最大化
- linux应用编程--简单常用函数
- 去掉字体加粗的实现
- hadoop3.0 分布式搭建/安装
- val设置获取属性值+each数组遍历
- 3415: Poi2013 Price List
- PHP使用array_intersect()函数求数组交集
- 带着实例继续学习(4):下拉框数据填充、组件生命周期、动态绑定事件
- C++11 (一)
- Android 进程启动源码解析