C语言基本输入输出学习笔记

1. 标准输入输出库

C语言的标准输入输出库提供了一组函数,用于实现基本的输入和输出操作。常用的输入输出函数定义在<stdio.h>头文件中。

2. 输出函数

2.1 printf 函数

printf函数用于格式化输出数据到标准输出设备(通常是屏幕)。

c 复制代码
#include <stdio.h>

int main() {
    int a = 10;
    float b = 3.14;
    char c = 'A';
    char str[] = "Hello, World!";
    printf("Integer: %d\n", a);
    printf("Float: %.2f\n", b);
    printf("Character: %c\n", c);
    printf("String: %s\n", str);
    return 0;
}
常用格式说明符
  • %d:整数
  • %f:浮点数
  • %c:字符
  • %s:字符串
  • %x:十六进制整数
  • %o:八进制整数

2.2 putchar 函数

putchar函数用于输出单个字符到标准输出设备。

c 复制代码
#include <stdio.h>

int main() {
    char c = 'A';
    putchar(c);
    putchar('\n');
    return 0;
}

2.3 puts 函数

puts函数用于输出字符串到标准输出设备,并自动在末尾添加换行符。

c 复制代码
#include <stdio.h>

int main() {
    char str[] = "Hello, World!";
    puts(str);
    return 0;
}

3. 输入函数

3.1 scanf 函数

scanf函数用于从标准输入设备(通常是键盘)读取格式化数据。

c 复制代码
#include <stdio.h>

int main() {
    int a;
    float b;
    char c;
    char str[100];
    printf("Enter an integer: ");
    scanf("%d", &a);
    printf("Enter a float: ");
    scanf("%f", &b);
    printf("Enter a character: ");
    scanf(" %c", &c); // 注意前面的空格以跳过前面的空白字符
    printf("Enter a string: ");
    scanf("%s", str);
    printf("You entered: %d, %.2f, %c, %s\n", a, b, c, str);
    return 0;
}
常用格式说明符
  • %d:读取整数
  • %f:读取浮点数
  • %c:读取字符
  • %s:读取字符串

3.2 getchar 函数

getchar函数用于从标准输入设备读取单个字符。

c 复制代码
#include <stdio.h>

int main() {
    char c;
    printf("Enter a character: ");
    c = getchar();
    printf("You entered: %c\n", c);
    return 0;
}

3.3 gets 函数

gets函数用于从标准输入设备读取字符串,直到遇到换行符为止。注意,gets函数存在缓冲区溢出问题,不建议使用。

c 复制代码
#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    gets(str);
    printf("You entered: %s\n", str);
    return 0;
}
安全替代:fgets 函数

fgets函数是gets函数的安全替代,用于从标准输入设备读取字符串,指定最大读取字符数。

c 复制代码
#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    printf("You entered: %s\n", str);
    return 0;
}

4. 文件输入输出

4.1 文件指针

文件指针是一个指向FILE类型的指针,用于标识文件流。

c 复制代码
#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("example.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }
    // 进行文件操作
    fclose(fp);
    return 0;
}

4.2 文件打开与关闭

  • fopen函数用于打开文件,返回文件指针。
  • fclose函数用于关闭文件。
c 复制代码
FILE *fopen(const char *filename, const char *mode);
int fclose(FILE *stream);
文件打开模式
  • "r":以只读模式打开文件
  • "w":以写入模式打开文件(文件不存在则创建,存在则清空)
  • "a":以追加模式打开文件(文件不存在则创建)
  • "r+":以读写模式打开文件
  • "w+":以读写模式打开文件(文件不存在则创建,存在则清空)
  • "a+":以读写模式打开文件(文件不存在则创建)

4.3 文件读写操作

读写单个字符
  • fgetc函数用于从文件中读取单个字符。
  • fputc函数用于向文件写入单个字符。
c 复制代码
int fgetc(FILE *stream);
int fputc(int c, FILE *stream);
c 复制代码
#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("example.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }
    char c;
    while ((c = fgetc(fp)) != EOF) {
        putchar(c);
    }
    fclose(fp);
    return 0;
}
读写字符串
  • fgets函数用于从文件中读取字符串。
  • fputs函数用于向文件写入字符串。
c 复制代码
char *fgets(char *str, int n, FILE *stream);
int fputs(const char *str, FILE *stream);
c 复制代码
#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("example.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }
    char str[100];
    while (fgets(str, sizeof(str), fp) != NULL) {
        printf("%s", str);
    }
    fclose(fp);
    return 0;
}
读写格式化数据
  • fprintf函数用于向文件写入格式化数据。
  • fscanf函数用于从文件中读取格式化数据。
c 复制代码
int fprintf(FILE *stream, const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
c 复制代码
#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("example.txt", "w");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }
    int a = 10;
    float b = 3.14;
    fprintf(fp, "Integer: %d\n", a);
    fprintf(fp, "Float: %.2f\n", b);
    fclose(fp);
    return 0;
}
相关推荐
Terra.K1 天前
Java异常学习[特殊字符]
java·开发语言·学习
wuyk5551 天前
98.C语言易混难点:字符数组与字符串指针的底层差异
c语言·开发语言·c++·stm32·嵌入式硬件·算法
峥无1 天前
从0到1手撕红黑树:封装实现 my_map 与 my_set(SGI-STL 源码级深度解析)
开发语言·c++·笔记·算法·stl
wp123_11 天前
IPX8 防水 Type‑C连接器:安费诺 124018792112A 与 TONEVEE TY48087‑24A 技术梳理
c语言·开发语言
不会代码的小猴1 天前
3. 控件学习1
开发语言·c++·笔记·qt·算法
疯狂打码的少年1 天前
【数据结构】图的存储结构:邻接矩阵与邻接表
数据结构·笔记
蒸蒸yyyyzwd1 天前
cpp 选手准备秋招学习笔记 day10
笔记·面试·求职招聘
haerapicom1 天前
欠定方程里藏着哪几个非零项:OMP 稀疏恢复侦探笔记
笔记
程思扬1 天前
Android ANR实战排查:主线程SystemClock.sleep导致页面启动偶现无响应
android·笔记
zjnlswd1 天前
C#学习笔记4
笔记·学习·c#