单片机c语言+编程c语言_C编程语言简介

单片机c语言+编程c语言

C is probably the most widely known programming language. It is used as the reference language for computer science courses all over the world, and it’s probably the language that people learn the most in school among with Python and Java.

C可能是最广为人知的编程语言。 它被用作全世界计算机科学课程的参考语言,它可能是人们在学校中使用Python和Java学习最多的语言。

I remember it being my second programming language ever, after Pascal.

我记得它是继Pascal之后的第二种编程语言。

C is not just what students use to learn programming. It’s not an academic language. And I would say it’s not the easiest language, because C is a rather low level programming language.

C不仅仅是学生用来学习编程的工具。 这不是一门学术语言。 我会说这不是最简单的语言,因为C是一种相当底层的编程语言。

Today, C is widely used in embedded devices, and it powers most of the Internet servers, which are built using Linux. The Linux kernel is built using C, and this also means that C powers the core of all Android devices. We can say that C code runs a good portion of the entire world. Right now. Pretty remarkable.

如今,C被广泛用于嵌入式设备中,并为大多数使用Linux构建的Internet服务器提供支持。 Linux内核是使用C构建的,这也意味着C是所有Android设备的核心。 可以说C代码在整个世界中占有很大的份额。 马上。 相当了不起。

When it was created, C was considered a high level language, because it was portable across machines. Today we kind of give for granted that we can run a program written on a Mac on Windows or Linux, perhaps using Node.js or Python. Once upon a time, this was not the case at all. What C brought to the table was a language simple to implement, having a compiler that could be easily ported to different machines.

创建C时,它被认为是高级语言,因为它可跨机器移植。 今天,我们可以在Windows或Linux上运行在Mac上编写的程序,这也许是理所当然的,也许可以使用Node.js或Python。 曾几何时,事实并非如此。 C带来的是一种易于实现的语言,它具有可以轻松移植到不同机器的编译器。

I said compiler: C is a compiled programming language, like Go, Java, Swift or Rust. Other popular programming language like Python, Ruby or JavaScript are interpreted. The difference is consistent: a compiled language generates a binary file that can be directly executed and distributed.

我说过编译器:C是一种编译的编程语言,例如Go,Java,Swift或Rust。 其他流行的编程语言(如Python,Ruby或JavaScript)也可以进行解释。 区别是一致的:编译语言生成可以直接执行和分发的二进制文件。

C is not garbage collected. This means we have to manage memory ourselves. It’s a complex task and one that requires a lot of attention to prevent bugs, but it is also what makes C ideal to write programs for embedded devices like Arduino.

C不是垃圾收集。 这意味着我们必须自己管理内存。 这是一项复杂的任务,需要大量关注以防止错误,但这也是C语言为嵌入式设备(如Arduino)编写程序的理想之选。

C does not hide the complexity and the capabilities of the machine underneath. You have a lot of power, once you know what you can do.

C不会隐藏下面机器的复杂性和功能。 一旦您知道自己能做什么,就拥有很大的力量。

I want to introduce the first C program now, which we’ll call “Hello, World!”

我现在想介绍第一个C程序,我们将其称为“ Hello,World!”。

hello.c

你好ç

#include <stdio.h>

int main(void) {
	printf("Hello, World!");
}

Let’s describe the program source code: we first import the stdio library (the name stands for standard input-output library).

让我们描述程序源代码:我们首先导入stdio库(名称代表标准输入输出库)。

This library gives us access to input/output functions.

该库使我们可以访问输入/输出功能。

C is a very small language at its core, and anything that’s not part of the core is provided by libraries. Some of those libraries are built by normal programmers, and made available for others to use. Some other libraries are built into the compiler. Like stdio and others.

C是一种非常小的语言,其核心是任何不属于核心的语言。 其中一些库是由普通程序员构建的,并可供其他人使用。 编译器还内置了其他一些库。 像stdio和其他人一样。

stdio is the libraries that provides the printf() function.

stdio是提供printf()函数的库。

This function is wrapped into a main() function. The main() function is the entry point of any C program.

该函数包装在main()函数中。 main()函数是任何C程序的入口点。

But what is a function, anyway?

但是,什么是函数?

A function is a routine that takes one or more arguments, and returns a single value.

函数是一个接受一个或多个参数并返回单个值的例程。

In the case of main(), the function gets no arguments, and returns an integer. We identify that using the void keyword for the argument, and the int keyword for the return value.

对于main() ,该函数不获取任何参数,并返回一个整数。 我们确定使用void关键字作为参数,使用int关键字作为返回值。

The function has a body, which is wrapped in curly braces, and inside the body we have all the code that the function needs to perform its operations.

该函数有一个主体,包裹在花括号中,在主体内部,我们具有该函数执行其操作所需的所有代码。

The printf() function is written differently, as you can see. It has no return value defined, and we pass a string, wrapped in double quotes. We didn’t specify the type of the argument.

如您所见, printf()函数的编写方式有所不同。 它没有定义返回值,我们传递了一个用双引号引起来的字符串。 我们没有指定参数的类型。

That’s because this is a function invocation. Somewhere, inside the stdio library, printf is defined as

那是因为这是一个函数调用。 在stdio库中的某处, printf定义为

int printf(const char *format, ...);

You don’t need to understand what this means now, but in short, this is the definition and when we call printf("Hello, World!");, that’s where the function is ran.

您无需了解这意味着什么,但总之,这就是定义,当我们调用printf("Hello, World!"); ,那就是函数运行的地方。

The main() function we defined above:

我们上面定义的main()函数:

#include <stdio.h>

int main(void) {
	printf("Hello, World!");
}

will be ran by the operating system when the program is executed.

将在执行程序时由操作系统运行。

How do we execute a C program?

我们如何执行C程序?

As mentioned, C is a compiled language. To run the program we must first compile it. Any Linux or macOS computer already comes with a C compiler built-in. For Windows, you can use the Windows Subsystem for Linux (WSL).

如前所述,C是一种编译语言。 要运行该程序,我们必须首先对其进行编译。 任何Linux或macOS计算机都已经内置了C编译器。 对于Windows,可以使用Windows Linux子系统(WSL)。

In any case, when you open the terminal window you can type gcc, and this command should return you an error saying that you didn’t specify any file:

无论如何,当您打开终端窗口时,您可以键入gcc ,并且此命令应返回一个错误,提示您未指定任何文件:

That’s good. It means the C compiler is there, and we can start using it.

非常好。 这意味着C编译器在那里,我们可以开始使用它了。

Now type the program above into a hello.c file. You can use any editor, but for the sake of simplicity I’m going to use the nano editor in the command line:

现在,将上面的程序键入hello.c文件。 您可以使用任何编辑器,但是为了简单起见,我将在命令行中使用nano编辑器:

Type the program:

输入程序:

Now press ctrl-X to exit:

现在按ctrl-X退出:

Confirm by pressing the y key, then press enter to confirm the file name:

y键确认,然后按Enter确认文件名:

That’s it, we should be back to the terminal now:

就是这样,我们现在应该回到终端:

Now type

现在输入

gcc hello.c -o hello

The program should give you no errors:

该程序应该不会给您任何错误:

but it should have generated a hello executable. Now type

但是它应该已经生成了一个hello可执行文件。 现在输入

./hello

to run it:

运行它:

I prepend ./ to the program name, to tell the terminal that the command is in the current folder

我在程序名称前添加./ ,以告知终端命令位于当前文件夹中

Awesome!

太棒了!

Now if you call ls -al hello, you can see that the program is only 12KB in size:

现在,如果调用ls -al hello ,则可以看到该程序的大小只有12KB:

This is one of the pros of C: it’s highly optimized, and this is also one of the reasons it’s this good for embedded devices that have a very limited amount of resources.

这是C语言的优点之一:高度优化,这也是对资源非常有限的嵌入式设备如此之好的原因之一。

Check out some of my other tutorials on C:

查看我在C上的其他一些教程:

翻译自: https://flaviocopes.com/c-introduction/

单片机c语言+编程c语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值