欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c语言中的i/o_C语言中的基本I / O概念

程序员文章站 2022-07-14 08:14:26
...

c语言中的i/o

C is a small language, and the “core” of C does not include any Input/Output (I/O) functionality.

C是一个小的语言和“核心” Ç不包括任何输入/输出(I / O)功能。

This is not something unique to C, of course. It’s common for the language core to be agnostic of I/O.

当然,这不是C独有的。 语言核心通常与I / O无关。

In the case of C, Input/Output is provided to us by the C Standard Library via a set of functions defined in the stdio.h header file.

对于C语言,C标准库通过stdio.h头文件中定义的一组函数为我们提供了输入/输出。

You can import this library using:

您可以使用以下命令导入该库:

#include <stdio.h>

on top of your C file.

在您的C文件之上。

This library provides us, among many other functions:

该库为我们提供了许多其他功能:

  • printf()

    printf()

  • scanf()

    scanf()

  • sscanf()

    sscanf()

  • fgets()

    fgets()

  • fprintf()

    fprintf()

Before describing what those functions do, I want to take a minute to talk about I/O streams.

在描述这些功能之前,我想花一点时间讨论一下I / O流

We have 3 kinds of I/O streams in C:

我们在C中有3种I / O流:

  • stdin (standard input)

    stdin (标准输入)

  • stdout (standard output)

    stdout (标准输出)

  • stderr (standard error)

    stderr (标准错误)

With I/O functions we always work with streams. A stream is a high level interface that can represent a device or a file. From the C standpoint, we don’t have any difference in reading from a file or reading from the command line: it’s an I/O stream in any case.

使用I / O功能,我们始终可以处理流。 流是可以代表设备或文件的高级接口。 从C的角度来看,从文件读取或从命令行读取没有任何区别:无论如何,它都是I / O流。

That’s one thing to keep in mind.

这是要记住的一件事。

Some functions are designed to work with a specific stream, like printf(), which we use to print characters to stdout. Using its more general counterpart fprintf(), we can specify the stream to write to.

一些函数被设计为与特定的流一起使用,例如printf() ,我们将其用于将字符打印到stdout 。 使用更通用的对应fprintf() ,我们可以指定要写入的流。

Since I started talking about printf(), let’s introduce it now.

自从我开始谈论printf()以来,现在让我们对其进行介绍。

printf() (printf())

printf() is one of the first functions you’ll use when learning C programming.

printf()是学习C编程时要使用的第一个功能之一。

In its simplest usage form, you pass it a string literal:

在最简单的用法形式中,您为它传递一个字符串文字:

printf("hey!");

and the program will print the content of the string to the screen.

然后程序会将字符串的内容打印到屏幕上。

You can print the value of a variable, and it’s a bit tricky because you need to add a special character, a placeholder, which changes depending on the type of the variable. For example we use %d for a signed decimal integer digit:

您可以打印变量的值,这有点棘手,因为您需要添加一个特殊字符(占位符),该字符会根据变量的类型而变化。 例如,我们将%d用于带符号的十进制整数:

int age = 37;

printf("My age is %d", age);

We can print more than one variable by using commas:

我们可以使用逗号来打印多个变量:

int age_yesterday = 37;
int age_today = 36;

printf("Yesterday my age was %d and today is %d", age_yesterday, age_today);

There are other format specifiers like %d:

还有其他格式说明符,例如%d

  • %c for a char

    %c个字符

  • %s for a char

    %s为一个字符

  • %f for floating point numbers

    %f用于浮点数

  • %p for pointers

    %p的指针

and many more.

还有很多。

We can use escape characters in printf(), like \n which we can use to make the output create a new line.

我们可以在printf()使用转义字符,例如\n ,以便使输出创建新行。

scanf() (scanf())

printf() is used as an output function. I want to introduce an input function now, so we can say we can do all the I/O thing: scanf().

printf()用作输出函数。 我现在想介绍一个输入函数,因此可以说我们可以完成所有I / O事情: scanf()

This function is used to get a value from the user running the program, from the command line.

此函数用于从运行命令行的用户那里获取值。

We must first define a variable that will hold the value we get from the input:

我们必须首先定义一个变量,该变量将保存从输入中获得的值:

int age;

Then we call scanf() with 2 arguments: the format (type) of the variable, and the address of the variable:

然后我们用两个参数调用scanf() :变量的格式(类型)和变量的地址:

scanf("%d", &age);

If we want to get a string as input, remember that a string name is a pointer to the first character, so you don’t need the & character before it:

如果要获取字符串作为输入,请记住字符串名称是第一个字符的指针,因此您不需要在&字符之前:

char name[20];
scanf("%s", name);

Here’s a little program that uses both printf() and scanf():

这是一个同时使用printf()scanf()的小程序:

#include <stdio.h>

int main(void) {
  char name[20];
  printf("Enter your name: ");
  scanf("%s", name);
  printf("you entered %s", name);
}

Download my free C Handbook, and check out my premium Courses

下载我的免费C手册 ,并查看我的高级课程




翻译自: https://flaviocopes.com/c-input-output/

c语言中的i/o