前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言常用函数表

C语言常用函数表

作者头像
Homqyy
发布2024-08-23 20:05:33
1000
发布2024-08-23 20:05:33
举报
文章被收录于专栏:知行合一

(附)C语言常用函数表

任何一门语言的重点绝对不是语法或词汇(等效于编程的接口),它的唯一用途是沟通。因此编程语言是为了让计算机服务于我们而存在的。因此我们知其然,但不记其所以然,只要知道它的用途就可以了。

C语言三板斧:maninfo--help

输入和输出

接口

说明

头文件

int printf(const char *format, ...);

格式化字符串,并输出到屏幕上(标准输出)

stdio.h

int fprintf(FILE *stream, const char *format, ...);

格式化字符串,并输出到指定的文件中

stdio.h

int sprintf(char *str, const char *format, ...);

格式化字符串,并输出到指定的字符串中

stdio.h

int scanf(const char *format, ...);

从标准输入中读取格式化的数据

stdio.h

int fscanf(FILE *stream, const char *format, ...);

从指定的文件中读取格式化的数据

stdio.h

int sscanf(const char *str, const char *format, ...);

从指定的字符串中读取格式化的数据

stdio.h

格式化字符串

格式化类型

格式

说明

%d

有符号十进制整数

%u

无符号十进制整数

%o

无符号八进制整数

%x

无符号十六进制整数

%f

十进制浮点数

%e

指数形式的浮点数

%g

十进制或指数形式的浮点数

%c

字符

%s

字符串

%p

指针

%n

该参数存储到目前为止读取的字符数的值

%%

%字符

控制输出长度的修饰符

修饰符

说明

有效的类型

%m.n

m表示输出的最小长度,n表示小数点后的位数

%f %e %g

%m

m表示输出的最小长度

%d %u %o %x %f %e %g %c %s %p

比如:

  • %10.2f表示输出的最小长度为10,小数点后的位数为2
  • %10s表示输出的最小长度为10

控制输出对齐方式的修饰符

| 修饰符 | 说明 |---|---|---| | - | 左对齐 | | + | 输出符号(正号或负号) |

比如:

  • %-10.2f表示输出的最小长度为10,小数点后的位数为2,左对齐
  • %+10.2f表示输出的最小长度为10,小数点后的位数为2,输出符号

示例¶

printf
代码语言:javascript
复制
#include 

int main(void)
{
    printf("hello world\n");
    return 0;
}
  • 结果为:hello world
fprintf
代码语言:javascript
复制
#include 

int main(void)
{
    FILE *fp = fopen("test.txt", "w");

    fprintf(fp, "hello world\n");
    fclose(fp);

    return 0;
}
  • 结果为:test.txt文件中的内容为:hello world
sprintf
代码语言:javascript
复制
#include 

int main(void)
{
    char buf[64];

    sprintf(buf, "hello world\n");
    printf("%s", buf);

    return 0;
}
  • 结果为:hello world
scanf
代码语言:javascript
复制
#include 

int main(void)
{
    int num;

    scanf("%d", &num);
    printf("num = %d\n", num);

    return 0;
}
  • 输入:123
  • 结果为:num = 123
fscanf
代码语言:javascript
复制
#include 

int main(void)
{
    FILE *fp = fopen("test.txt", "r");
    int num;

    fscanf(fp, "%d", &num);
    printf("num = %d\n", num);

    fclose(fp);

    return 0;
}
  • test.txt文件中的内容为:123
  • 结果为:num = 123
sscanf
代码语言:javascript
复制
#include 

int main(void)
{
    char buf[64] = "123";
    int num;

    sscanf(buf, "%d", &num);
    printf("num = %d\n", num);

    return 0;
}
  • 结果为:num = 123

字符串

接口

说明

头文件

size_t strlen(const char *s);

返回字符串s的长度

string.h

char *strcpy(char *dest, const char *src);

将字符串src复制到dest

string.h

char *strncpy(char *dest, const char *src, size_t n);

将字符串src的前n个字符复制到dest

string.h

char *strcat(char *dest, const char *src);

将字符串src连接到dest的末尾

string.h

char *strncat(char *dest, const char *src, size_t n);

将字符串src的前n个字符连接到dest的末尾

string.h

int strcmp(const char *s1, const char *s2);

比较字符串s1和s2,相等返回0,s1 < s2返回-1,s1 > s2 返回 1

string.h

int strncmp(const char *s1, const char *s2, size_t n);

比较字符串s1和s2的前n个字符, 相等返回0,s1 < s2返回-1,s1 > s2 返回 1

string.h

char *strchr(const char *s, int c);

在字符串s中查找字符c的第一次出现的位置

string.h

char *strrchr(const char *s, int c);

在字符串s中查找字符c的最后一次出现的位置

string.h

char *strstr(const char *haystack, const char *needle);

在字符串haystack中查找字符串needle的第一次出现的位置

string.h

char *strtok(char *str, const char *delim);

- 分解字符串str为一组字符串,delim为分隔符; - 首次调用时需要传入str,后续调用传入NULL - 每一次调用返回一个字符串,如果没有更多的字符串则返回NULL - 该函数会修改str,将delim替换为\0,因此需要保证str是可修改的,如果必要的话需要备份str

string.h

示例

strlen
代码语言:javascript
复制
#include 
#include 

int main(void)
{
    int len = strlen("hello");

    printf("len = %d\n", len);

    return 0;
}
  • 结果为:len = 5
strcpy
代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char dst[10];

    strcpy(dst, "hello");
    printf("dst = %s\n", dst);

    return 0;
}
  • 结果为:dst = hello

strncpy

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char dst[10];

    strncpy(dst, "hello", 3);
    printf("dst = %s\n", dst);

    return 0;
}
  • 结果为:dst = hel

strcat

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char dst[20] = "hello";

    strcat(dst, " world");
    printf("dst = %s\n", dst);

    return 0;
}
  • 结果为:dst = hello world

strncat

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char dst[20] = "hello";

    strncat(dst, " world", 3);
    printf("dst = %s\n", dst);

    return 0;
}
  • 结果为:dst = hello wo

strcmp

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    int rc = strcmp("hello", "hello world");

    printf("rc = %d\n", rc);

    return 0;
}
  • 结果为:rc = -1

strncmp

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    int rc = strncmp("hello", "hello world", 5);

    printf("rc = %d\n", rc);

    return 0;
}
  • 结果为:rc = 0

strchr

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char *pos = strchr("hello", 'l');
    printf("pos = %s\n", pos);
    return 0;
}
  • 结果为:pos = llo

strrchr

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char *pos = strrchr("hello", 'l');

    printf("pos = %s\n", pos);

    return 0;
}
  • 结果为:pos = lo

strstr

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char *pos = strstr("hello", "ll");

    printf("pos = %s\n", pos);

    return 0;
}
  • 结果为:pos = llo

strtok

代码语言:javascript
复制
#include 
#include 

int main(void)
{
    char buf[64] = "hello world";
    char *token = strtok(buf, " ");

    while (token)
    {
        printf("%s\n", token);
        token = strtok(NULL, " ");
    }

    return 0;
}
  • 结果为:
代码语言:javascript
复制
hello
world
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-01-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • (附)C语言常用函数表
    • 输入和输出
      • 格式化字符串
    • 控制输出长度的修饰符
      • 控制输出对齐方式的修饰符
        • 示例¶
      • 字符串
        • 示例
        • strncpy
        • strcat
        • strncat
        • strcmp
        • strncmp
        • strchr
        • strrchr
        • strstr
        • strtok
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档