C 语言中的头文件“stdio.h”和“stdlib.h”
stdio.h
头文件 stdio.h 代表标准输入输出。它包含与输入/输出函数相关的信息。
下表显示了 C 语言中 stdio.h 中的一些函数:
| 序号 | 函数及描述 |
|---|---|
| 1 | printf() 用于在输出屏幕上打印字符串、整数、字符等。 |
| 2 | scanf() 从键盘读取字符、字符串、整数等。 |
| 3 | getc() 从文件读取字符。 |
| 4 | putc() 将字符写入文件。 |
| 5 | fopen() 打开文件,所有文件处理函数都在 stdio.h 头文件中定义。 |
| 6 | fclose() 关闭已打开的文件。 |
| 7 | remove() 删除文件。 |
| 8 | fflush() 刷新文件。 |
以下是一个 C 语言中 stdio.h 的示例:
示例
#include<stdio.h>
int main () {
char val;
printf("Enter the character: \n");
val = getc(stdin);
printf("Character entered: ");
putc(val, stdout);
return(0);
}输出
以下是输出结果
Enter the character: s Character entered: s
stdlib.h
头文件 stdlib.h 代表标准库。它包含内存分配/释放函数的信息。
下表显示了 C 语言中 stdlib.h 中的一些函数:
| 序号 | 函数及描述 |
|---|---|
| 1 | malloc() 在程序执行期间分配内存。 |
| 2 | free() 释放已分配的内存。 |
| 3 | abort() 终止 C 程序。 |
| 4 | exit() 终止程序,不返回值。 |
| 5 | atol() 将字符串转换为长整型。 |
| 6 | atoll() 将字符串转换为长长整型。 |
| 7 | atof() 将字符串转换为浮点数。 |
| 8 | rand() 返回一个随机整数值 |
以下是一个 C 语言中 stdlib.h 的示例:
示例
#include <stdio.h>
#include<stdlib.h>
int main() {
char str1[20] = "53875";
char str2[20] = "367587938";
char str3[20] = "53875.8843";
long int a = atol(str1);
printf("String to long int : %d\n", a);
long long int b = atoll(str2);
printf("String to long long int : %d\n", b);
double c = atof(str3);
printf("String to long int : %f\n", c);
printf("The first random value : %d\n", rand());
printf("The second random value : %d", rand());
return 0;
}输出
以下是输出结果
String to long int : 53875 String to long long int : 367587938 String to long int : 53875.884300 The first random value : 1804289383 The second random value : 846930886
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP