博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言中的i/o_C语言中的基本I / O概念
阅读量:2507 次
发布时间:2019-05-11

本文共 4023 字,大约阅读时间需要 13 分钟。

c语言中的i/o

C is a small language, and the “core” of 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 

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 
int main(void) { char name[20]; printf("Enter your name: "); scanf("%s", name); printf("you entered %s", name);}

翻译自:

c语言中的i/o

转载地址:http://jmmgb.baihongyu.com/

你可能感兴趣的文章
线程基础知识归纳
查看>>
CArray 的两种方式与类中包含指针情况
查看>>
ElasticSearch 自定义排序处理
查看>>
域的建立过程
查看>>
使用installer安装kbengine
查看>>
IOS 开发didFinishLaunchingWithOptions 设置启动View
查看>>
MyBank(自助银行)
查看>>
python机器学习-sklearn挖掘乳腺癌细胞(二)
查看>>
javascript中的函数节流和函数去抖
查看>>
异步函数的串行执行和并行执行
查看>>
Import Solution Error code :0x80048426
查看>>
Spring的注解@Qualifier小结
查看>>
目前最新版本ActiveMQ 5.15.3 和JDK版本有关的问题
查看>>
hdu 4513 吉哥系列故事——完美队形II
查看>>
ECSHOP让产品浏览历史按照先后进行排序
查看>>
解决小程序中 cover-view无法盖住canvas的问题,仅安卓真机出现
查看>>
C# 获取数组的内存地址
查看>>
职场规则五
查看>>
跟我一起学WCF(1)——MSMQ消息队列
查看>>
京东联盟采集规则
查看>>