

函数声明 | 函数功能 |
|---|---|
| 它是 C 语言标准库中的函数,用于使当前进程挂起一定的时间。在挂起期间,操作系统会将该进程从调度队列中移除,直到指定的时间过去为止。 |
| 它是 |
| 它是 |
| 用于发出声音, |
| 它是在 Windows 平台上使用的函数,用于启动另一个程序,并等待该程序运行结束后再继续执行本程序 |
| 它是在 |
| 用于将格式化的字符串输出到指定的缓冲区中 |
| 用于将格式化的字符串输出到指定的缓冲区中,类似于 |
| 计算 x 的平方根(double) |
| 计算 x 的平方根 (float) |
| 计算 x 的平方根 (long double) |
| 用于初始化伪随机数生成器 |
| 用于从字符串中读取数据并进行格式化转换 |
| 用于获取文件或目录的属性信息,例如文件大小、创建时间、修改时间等。这些属性信息都被保存在一个名为 |
| 它是是 Unix/Linux 系统中的一个系统调用函数,用于设置系统时间 |
| 用于复制一个字符串到另一个字符串缓冲区,并返回目标字符串的结尾指针 |
| 用于将一个字符串拼接到另一个字符串的末尾 |
| 用于查找字符串中第一次出现指定字符的位置,并返回该位置的指针 |
| 用于比较两个字符串是否相等 |
| 用于将一个字符串复制到另一个字符串缓冲区中 |
| 用于查找字符串中第一次出现指定字符集合中任何字符的位置,并返回该位置的索引 |
函数声明 | 函数功能 |
|---|---|
| 它是 C 语言标准库中的函数,用于使当前进程挂起一定的时间。在挂起期间,操作系统会将该进程从调度队列中移除,直到指定的时间过去为止。 |
| 它是 |
sleep 函数参数:
Sleep 函数参数:
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Start sleeping...\n");
sleep(5);
printf("Wake up!\n");
return 0;
}在使用 sleep() 函数时,将会使当前线程或者进程暂停指定的时间,以便给其他进程或线程执行机会,同时也可以用来控制程序的运行速度。
虽然 sleep() 函数很简单,但是需要注意以下几点:
sleep() 的精度并不高,它所挂起的时间可能会略微超过要求的时间。sleep() 函数是阻塞式的,即在调用 sleep() 函数期间,该进程不能进行任何其他操作,包括响应信号等。sleep() 函数期间,如果发生信号,那么 sleep() 函数将被中断,该进程将继续运行。#include <stdio.h>
#include <Windows.h>
int main()
{
printf("Start sleeping...\n");
Sleep(5000); // 暂停 5 秒钟
printf("Wake up!\n");
return 0;
}由于 Sleep() 函数是阻塞式的,因此该函数调用期间,当前线程将被阻塞。在函数调用结束后,该线程将恢复运行。
在 Windows 系统下使用 Sleep() 函数时,需要注意以下几点:
Sleep() 函数以毫秒为单位指定时间,精度比 sleep() 函数更高。Sleep() 函数期间,当前线程将被阻塞,不能进行任何其他操作,包括响应信号等。Sleep() 函数期间,如果发生信号,那么 Sleep() 函数将被中断,该线程将继续运行。

函数声明 | 函数功能 |
|---|---|
| 它是 |
参数:
_O_RDONLY:只读方式打开文件_O_WRONLY:只写方式打开文件_O_RDWR:读写方式打开文件_O_APPEND:在文件末尾追加数据_O_CREAT:如果文件不存在,则创建文件_O_TRUNC:如果文件已存在,清空文件内容_O_EXCL:与 _O_CREAT 配合使用,如果文件已经存在则打开失败_SH_DENYRW:独占方式打开文件,其他进程不能读取或写入该文件_SH_DENYWR:共享读取方式打开文件,其他进程不能写入该文件_SH_DENYRD:共享写入方式打开文件,其他进程不能读取该文件_SH_DENYNO:共享方式打开文件,其他进程可以读取和写入该文件_S_IWRITE:文件可写_S_IREAD:文件可读_O_CREAT 参数,则需要指定文件的访问权限#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#define _SH_DENYNO 0x40
int main()
{
int handle;
char buffer[1024];
// 打开文件并读取数据
handle = sopen("output.bin", _O_RDONLY, _SH_DENYNO, _S_IREAD);
if (handle == -1)
{
printf("Failed to open file!\n");
return 1;
}
read(handle, buffer, sizeof(buffer));
printf("File content: %s\n", buffer);
// 关闭文件句柄
close(handle);
return 0;
}指定文件共享模式,如果没有对应的宏常量,则可以定义如下:
#define _SH_DENYRW 0x10
#define _SH_DENYWR 0x20
#define _SH_DENYRD 0x30
#define _SH_DENYNO 0x40

函数声明 | 函数功能 |
|---|---|
| 用于发出声音 |
|
|
参数:
#include <stdio.h>
#include <windows.h>
int main()
{
printf("Playing sound...\n");
sound(1000); // 发出 1000 Hz 音调
Sleep(5000); // 等待 5 秒钟
nosound(); // 停止发声
printf("Sound stopped.\n");
return 0;
}Windows 下如果上述出现 error: 'sound' was not declared in this scope,可以使用如下:
#include <stdio.h>
#include <windows.h>
int main()
{
printf("Playing sound...\n");
Beep(1000, 5000); // 发出 1000 Hz 音调,持续 5 秒钟
printf("Sound stopped.\n");
return 0;
}注意: 在 Windows 平台上建议使用 Beep() 函数代替 sound() 函数,因为 Beep() 函数不需要特殊的硬件支持,并且可移植性更好。
函数声明 | 函数功能 |
|---|---|
| 它是在 Windows 平台上使用的函数,用于启动另一个程序,并等待该程序运行结束后再继续执行本程序 |
参数:
P_WAIT 或 P_NOWAITNULL 结尾#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}#include <process.h>
#include <stdio.h>
int main(void)
{
int result;
// 要执行的程序名和参数列表
const char* cmdname = "test.exe";
const char* arg0 = NULL;
// 执行程序
result = spawnl(P_WAIT, cmdname, cmdname, arg0, NULL);
if (result == -1)
{
perror("Error from spawnl");
exit(1);
}
return 0;
}如果在使用 spawnl() 函数时遇到了 "Error from spawnl: Invalid argument" 错误,有可能是由于参数传递不正确或要执行的程序不存在等原因导致的。
以下是一些可能导致该错误的情况:
cmdname 参数包含非法字符或格式不正确。NULL 结尾。
函数声明 | 函数功能 |
|---|---|
| 它是在 |
参数:
P_WAIT 或 P_NOWAITNULL 结尾#include <process.h>
#include <processenv.h>
#include <stdio.h>
int main(int argc, char *argv[], char **envp)
{
printf("SubEnvTest Command line arguments:\n");
for (int i=0; i<argc; i++)
printf("[%d] : %s\n", i, argv[i]);
printf("exec %s, Hello, %s\n", argv[0], argv[1]);
for (int i = 0; envp[i] != NULL; i++)
{
printf("%s\n", envp[i]);
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <process.h>
int main(int argc, char *argv[], char *envp[])
{
int result;
// 启动 SubEnvTest.exe,并传递当前环境变量
result = spawnle(P_WAIT, "SubEnvTest.exe", "SubEnvTest.exe", NULL, envp);
if (result == -1)
{
printf("Error: %d\n", errno);
return 1;
}
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 用于将格式化的字符串输出到指定的缓冲区中 |
参数:
#include <stdio.h>
int main()
{
char buffer[128];
int value = 123;
// 将整数转换为字符串并保存到 buffer 中
sprintf(buffer, "The value is %d.", value);
printf("%s\n", buffer);
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 用于将格式化的字符串输出到指定的缓冲区中,类似于 |
参数:
#include <stdio.h>
int main()
{
char buffer[5];
int value = 123456;
// 将整数转换为字符串并保存到 buffer 中,最大可写入长度为 sizeof(buffer) - 1
snprintf(buffer, sizeof(buffer), "%d", value);
printf("%s\n", buffer); // 输出 "1234"
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 计算 x 的平方根(double) |
| 计算 x 的平方根 (float) |
| 计算 x 的平方根 (long double) |
参数:
#include <stdio.h>
#include <math.h>
int main()
{
double x = 2.0;
float y = 3.0f;
long double z = 4.0L;
printf("sqrt(%.1f) = %.2f\n", x, sqrt(x));
printf("sqrtf(%.1f) = %.2f\n", y, sqrtf(y));
printf("sqrtl(%.1Lf) = %.2Lf\n", z, sqrtl(z));
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 用于初始化伪随机数生成器 |
参数:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
// 使用当前时间作为种子值
srand(time(NULL));
// 生成 10 个随机数并输出到控制台
for (i = 0; i < 10; i++)
{
printf("%d ", rand());
}
printf("\n");
return 0;
}注意: 如果不设置种子值,则每次程序运行时都会得到相同的随机数序列。因此,我们在实际开发中,常常使用时间戳或其他随机值来作为种子值,以确保生成的随机数具有更好的随机性。

函数声明 | 函数功能 |
|---|---|
| 用于从字符串中读取数据并进行格式化转换 |
参数:
#include <stdio.h>
int main()
{
char str[] = "hello world 123";
char buf[16];
int num;
// 从字符串中读取一个字符串和一个整数
if (sscanf(str, "%s %*s %d", buf, &num) == 2)
{
printf("String: %s\n", buf);
printf("Number: %d\n", num);
}
return 0;
}在上述的示例代码中,我们使用 sscanf() 函数从字符串 "hello world 123" 中读取一个字符串和一个整数,并输出到控制台。
注意: 在格式字符串中,%s 表示读取一个字符串,%d 表示读取一个整数。另外,%*s 表示读取并忽略一个字符串。

函数声明 | 函数功能 |
|---|---|
| 用于获取文件或目录的属性信息,例如文件大小、创建时间、修改时间等。这些属性信息都被保存在一个名为 |
参数:
struct stat 结构体的指针,用于存储获取到的属性信息返回值:
0;-1 并设置相应的错误码(存储在 errno 变量中)#include <stdio.h>
#include <sys/stat.h>
int main()
{
struct stat file_stat;
// 获取文件属性信息
if (stat("test.txt", &file_stat) == 0) {
printf("File size: %ld bytes\n", file_stat.st_size);
printf("Creation time: %ld\n", file_stat.st_ctime);
printf("Modification time: %ld\n", file_stat.st_mtime);
}
return 0;
}

函数声明 | 函数功能 |
|---|---|
| 它是是 Unix/Linux 系统中的一个系统调用函数,用于设置系统时间 |
参数:
返回值:
0;-1 并设置相应的错误码(存储在 errno 变量中)#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
time_t t;
// 获取当前时间
time(&t);
// 输出当前时间
printf("Current time: %s", ctime(&t));
// 设置系统时间为 2022 年 1 月 1 日
struct tm new_time;
new_time.tm_sec = 0;
new_time.tm_min = 0;
new_time.tm_hour = 0;
new_time.tm_mday = 1;
new_time.tm_mon = 0;
new_time.tm_year = 122;
new_time.tm_wday = 6;
new_time.tm_yday = 0;
new_time.tm_isdst = -1;
t = mktime(&new_time);
stime(&t);
// 短暂等待,确保时间设置完成
sleep(1);
// 再次输出当前时间
time(&t);
printf("New time: %s", ctime(&t));
return 0;
}在如上的示例代码中,
time() 函数获取当前时间,并输出到控制台;2022 年 1 月 1 日,并等待一秒钟以确保时间设置完成。注意: stime() 函数只能在 Linux/Unix 系统上使用,并且需要 root 权限才能调用。另外,在修改系统时间时应谨慎行事,以避免对系统和应用程序造成不可预料的影响。
函数声明 | 函数功能 |
|---|---|
| 用于复制一个字符串到另一个字符串缓冲区,并返回目标字符串的结尾指针 |
参数:
返回值: 一个指向目标字符串结尾的指针
#include <stdio.h>
#include <string.h>
int main()
{
char src[] = "Hello world";
char dest[32];
char *end = stpcpy(dest, src);
printf("Source string: %s\n", src);
printf("Destination string: %s\n", dest);
printf("End pointer: %p\n", end);
return 0;
}在上述的示例代码中,我们使用 stpcpy() 函数将源字符串 "Hello world" 复制到目标字符串 dest 中,并输出两个字符串以及目标字符串的结尾指针。
注意: stpcpy() 函数只能在支持 ISO C99 或 POSIX.1-2001 标准的系统上使用,对于其他系统,可能需要使用 strcpy() 函数代替。此外,应始终确保目标字符串缓冲区具有足够的空间来存储源字符串,以避免发生缓冲区溢出。
函数声明 | 函数功能 |
|---|---|
| 用于将一个字符串拼接到另一个字符串的末尾 |
参数:
返回值: 一个指向目标字符串结尾的指针
#include <stdio.h>
#include <string.h>
int main()
{
char str1[32] = "Hello";
char str2[] = " world!";
printf("Before: str1=%s str2=%s\n", str1, str2);
strcat(str1, str2);
printf("After: str1=%s\n", str1);
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 用于查找字符串中第一次出现指定字符的位置,并返回该位置的指针 |
参数:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello world";
char* p;
p = strchr(str, 'o');
if (p != NULL) {
printf("Found character '%c' at position %ld\n", *p, p - str);
}
else {
printf("Character not found.\n");
}
return 0;
}注意: strchr() 函数只能查找单个字符,如果要查找一个子字符串,应使用 strstr() 函数代替。另外,在查找字符时,需要将字符转换为整数值传递给 strchr() 函数,以避免发生类型错误。

函数声明 | 函数功能 |
|---|---|
| 用于比较两个字符串是否相等 |
参数:
返回值: 一个整数,表示两个字符串之间的大小关系
str1 小于 str2,则返回负整数;str1 大于 str2,则返回正整数;str1 等于 str2,则返回 0。#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hello World";
char str2[] = "hello world";
int result = strcmp(str1, str2);
if (result < 0) {
printf("'%s' is less than '%s'\n", str1, str2);
}
else if (result 0) {
printf("'%s' is greater than '%s'\n", str1, str2);
}
else {
printf("'%s' is equal to '%s'\n", str1, str2);
}
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 用于将一个字符串复制到另一个字符串缓冲区中 |
参数:
返回值: 一个指向目标字符串结尾的指针
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello world";
char dest[32];
strcpy(dest, src);
printf("Source string: %s\n", src);
printf("Destination string: %s\n", dest);
return 0;
}注意: strcpy() 函数只能用于复制以 \0 结尾的字符串,否则可能导致未定义的行为或内存损坏。在调用 strcpy() 函数之前,应确保目标字符串缓冲区具有足够的空间来容纳源字符串,以避免发生缓冲区溢出。

函数声明 | 函数功能 |
|---|---|
| 用于查找字符串中第一次出现指定字符集合中任何字符的位置,并返回该位置的索引 |
参数:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello world";
char charset[] = "owd";
size_t index = strcspn(str, charset);
if (index != strlen(str))
{
printf("Found character '%c' at position %ld\n", str[index], index);
}
else
{
printf("No matching characters found.\n");
}
return 0;
}在上述的示例代码中,我们使用 strcspn() 函数在字符串 "Hello world" 中查找字符集合 "owd" 中的任何字符,并输出找到的字符及其在字符串中的位置索引。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。