做题目时常用的一些C函数

时间:2022-12-09 19:27:10

C语言输入输出函数

printf族函数

#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

这又是一大堆的函数,其实搞懂了一个,其余的东西都好懂了。
printf函数向标准输出输出数据!而fprintf将数据输出到FILE指针指向的文件,sprintf将数据输出到str指针指向的字符串。snprintf多了一个n,说明这是一个安全版本的sprintf函数,可以避免向str指向的字符数组写入过多的数据,前提是你要将size填充为str指向字符串的长度!

我这里只注重于描述format的输出格式!
参数如下:
%[flag][fldwidth][precision][lenmodifier]convtype
flag是标志,可以取下表的值:

标志 说明
撇号将整数按照千分位分组字符
- 在字段内左对齐输出
+ 总是显示带符号转换的正负号
(空格) 如果第一个字符不是正负号,则在其前面加上一个空格
# 制定另一种转换形式,例如,对于十六进制格式,加上0x前缀
0 添加前导0进行填充

fldwidth说明最小的字符宽度,转换后参数字符数若小于宽度,则多余字符用空格来填充,当然,如果设置了0标志,就用0来填充。

precision用来指定精度,这个东西一般只有浮点数才使用,用与指定浮点数转换成小数之后小数点后面的最少的位数。

lenmodifier用于指定参数的长度,实际上我们用的并不是很多,我也不打算在这里浪费时间。

convtype为必选的参数,它可以取下面的值:

字符 对应数据类型 含义
d / i int 接受整数值并将它表示为有符号的十进制整数,i是老式写法
o unsigned int 无符号8进制整数(不输出前缀0
u unsigned int 无符号10进制整数
x / X unsigned int 无符号16进制整数,x对应的是abcdefX对应的是ABCDEF(不输出前缀0x)
f(lf) float(double) 单精度浮点数用f,双精度浮点数用lf(尤其scanf不能混用)
e / E double 科学计数法表示的数,此处"e"的大小写代表在输出时用的“e”的大小写
g / G double 使用以上两种中最短的形式,大小写的使用同%e和%E
c char 字符型。可以把输入的数字按照ASCII码相应转换为对应的字符
s / S char * / wchar_t * 字符串。输出字符串中的字符直至字符串中的空字符(字符串以'\0'结尾,这个'\0'即空字符)
p void * 16进制形式输出指针
n int * 到此字符之前为止,一共输出的字符个数,不输出文本
% 无输入 不进行转换,输出字符'%'(百分号)本身
m 打印errno值对应的出错内容,(例: printf("%m\n"); )

scanf族函数

 #include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);

我们使用的格式如下:
%[*][fldwidth][m][lenmodifier]convtype
可选的*号用于抑制转换,按照转换说明的其余部分进行转换,但是结果不放入参数中!
fldwidth说明最大宽度,lenmodifier说明要转化结果复制的参数的大小,这个参数连同m都不常用,所以这里不会过多的涉及这两个参数。
convtype字段类似于printf族的转换类型字段,可以取的值如下:

c 读单字符
d 读十进制整数
i 读十进制、八进制、十六进制整数

a , A ,e ,E,f ,F ,g ,G读浮点值

o 读八进制数
s 读字符串
x 读十六进制数
X 读十六进制数
p 读指针值
n 至此已读入值的等价字符数
u 读无符号十进制整数
[str] str代表单个字符或者字符串,这个作用是扫描str代表的字符串集合,直到扫描到不在str中的字符为止
[^str] str代表单个字符或者字符串,与上面的相反,这里是匹配除了str指代的字符串集合外的字符,直到遇到了在str集合中的字符为止
% 读 % 符号(百分号)

我们来练一练!
下面是一段很简单的代码:

#include <stdio.h>

int main()
{
    int a, b;
    char str[1024];
    scanf("%d %d %s", &a, &b, str);
    printf("a = %d, b = %d, str = \"%s\"\n", a, b, str);
    return 0;
}

我这么玩:
做题目时常用的一些C函数

我们在3445的后面输入了空格,然后字符str里面就真的只有3445了,到底应该怎样才能是后面的45连同45前面的空格都输入str中呢?我们这么干:

#include <stdio.h>
int main()
{
    int a, b;
    char str[1024];
    scanf("%d %d %[^\n]", &a, &b, str);
    printf("a = %d, b = %d, str = \"%s\"\n", a, b, str);
    return 0;
}

看结果:
做题目时常用的一些C函数
看清楚了我们将%s替换成了%[^\n],后者的意思是一直读,读到\n为止,也就是换行符为止!所以从3445 45就被读取了出来!

然后是*的使用:

#include <stdio.h>

int main()
{
    int a, b = 0;
    char str[1024];
    scanf("%d %*d %[^\r\n]", &a, str);
    printf("a = %d, b = %d, str = \"%s\"\n", a, b, str);
    return 0;
}

做题目时常用的一些C函数
看到没有*代表跳过这个参数!


关于filedwidth字符的运用:

#include <stdio.h>

int main()
{
    int a, b = 0;
    char str[1024];
    scanf("%3d%3d", &a, &b);
    printf("a = %d, b = %d, str = \"%s\"\n", a, b, str);
    return 0;
}

做题目时常用的一些C函数
看到了吧!

sacnf族的函数还有一些成员,很好用!

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

这两个函数和sacnf唯一个区别就是scanf从标准输入读入数据,fscanfFILE指针指向的文件读取数据,sscanfstr字符数组读取数据。


C语言string.h的常用函数

下面是c语言中string.h中的一些常用的函数,不想用c++里面坑爹的string。这里总结一下,以便将来查询。

strcpy用于复制字符串

首先是两个用于复制的函数!

include <string.h>
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
The strcpy() function copies the string pointed to by src, including the terminating null byte(‘\0’), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null terminated.

A simple implementation of strncpy() might be:

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

    for (i = 0 ; i < n && src[i] != '\0' ; i++)
        dest[i] = src[i];
    for ( ; i < n ; i++)
        dest[i] = '\0';

    return dest;
}

RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.

NOTES
Some programmers consider strncpy() to be inefficient and error prone. If the programmer knows (i.e., includes code to test!) that the size of dest is greater than the length of src, then strcpy() can be used.

Programmers often prevent this mistake by forcing termination as follows:

strncpy(buf, str, n);
if (n > 0)
   buf[n - 1]= '\0';

strstr用于查找的子串

#include <string.h>
char *strstr(const char *haystack, const char *needle);

DESCRIPTION
The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating ‘\0’ characters are not compared.

RETURN VALUE
These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.


strlen函数用于检测字符长度

#include <string.h>
size_t strlen(const char *s);

DESCRIPTION
The strlen() function calculates the length of the string s, not including the terminating ‘\0’ character.
RETURN VALUE
The strlen() function returns the number of characters in s.
然后是用于用于字符串拼接的strcat函数!

 #include <string.h>
 char *strcat(char *dest, const char *src);
 char *strncat(char *dest, const char *src, size_t n);

DESCRIPTION
The strcat() function appends the src string to the dest string, overwriting the null byte (‘\0’) at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.

The strncat() function is similar, except that
* it will use at most n characters from src; and
* src does not need to be null terminated if it contains n or more characters.

If src contains n or more characters, strncat() writes n+1 characters to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.

A simple implementation of strncat() might be:

  char*
  strncat(char *dest, const char *src, size_t n)
  {
      size_t dest_len = strlen(dest);
      size_t i;
      for (i = 0 ; i < n && src[i] != '\0' ; i++)
          dest[dest_len + i] = src[i];
      dest[dest_len + i] = '\0';
      return dest;
   }

RETURN VALUE
The strcat() and strncat() functions return a pointer to the resulting string dest.


用于比较两个字符串的函数strcmp

#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);

DESCRIPTION
The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

The strncmp() function is similar, except it only compares the first (at most) n characters of s1 and s2.

RETURN VALUE
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

差不多就是这些函数,非常好用,也简单,可以自己实现。