跳转至

(附)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

#include <stdio.h>

int main(void)
{
    printf("hello world\n");
    return 0;
}
  • 结果为:hello world

fprintf

#include <stdio.h>

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

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

    return 0;
}
  • 结果为:test.txt文件中的内容为:hello world

sprintf

#include <stdio.h>

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

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

    return 0;
}
  • 结果为:hello world

scanf

#include <stdio.h>

int main(void)
{
    int num;

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

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

fscanf

#include <stdio.h>

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

#include <stdio.h>

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); 比较字符串s1s2,相等返回0s1 < s2返回-1s1 > s2 返回 1 string.h
int strncmp(const char *s1, const char *s2, size_t n); 比较字符串s1s2的前n个字符, 相等返回0s1 < s2返回-1s1 > 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

#include <stdio.h>
#include <string.h>

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

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

    return 0;
}
  • 结果为:len = 5

strcpy

#include <stdio.h>
#include <string.h>

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

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

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

strncpy

#include <stdio.h>
#include <string.h>

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

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

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

strcat

#include <stdio.h>
#include <string.h>

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

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

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

strncat

#include <stdio.h>
#include <string.h>

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

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

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

strcmp

#include <stdio.h>
#include <string.h>

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

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

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

strncmp

#include <stdio.h>
#include <string.h>

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

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

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

strchr

#include <stdio.h>
#include <string.h>

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

strrchr

#include <stdio.h>
#include <string.h>

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

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

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

strstr

#include <stdio.h>
#include <string.h>

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

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

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

strtok

#include <stdio.h>
#include <string.h>

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

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

    return 0;
}
  • 结果为:
hello
world

评论