很少有编程语言具有 C 的多功能性和实用性,但语言本身通常看起来令人生畏,尤其是对于新手而言。好消息是 C 语言并不像看起来那么难学,而且有许多免费资源可以(numerous resources available for free)帮助您入门。
C 编程的历史
C 编程由Dennis M. Ritchie于 1972 年开发。此后,有几种语言从核心 C 语言分支出来,包括 C++ 和Objective C。该语言是作为一种通用语言创建的,可用于各种应用程序,它已经实现了这一目标。
事实上,C 是现存最广泛使用的编程语言(C is one of the most widely used programming languages)之一,如果不是最广泛使用的话。为什么是C,你可能会问?它继承了以前称为 B 的编程语言。在现代世界中,C 用于系统编程而不是软件编程。
为什么要学习 C?
许多编程世界的新手首先学习Java或Python。这些是一些最流行的现代语言,但 C 具有同样多的实用性。业余编码人员经常惊讶地发现 C 由于其结构而易于学习。它能够生成高效、精简的程序,并且可以比其他语言更好地处理较低级别的活动。
也许 C 的最大优势在于它可以在各种平台上编译。事实上,Unix完全是用 C 语言编写的。
C 编程基础
在我们继续学习本 C 初学者教程之前,请先了解:编程语言易于学习,但难以掌握。影响语言工作方式的因素太多了,一篇文章甚至不可能涵盖最基本的方面。本指南将帮助您找到所需的资源并教您核心概念,以便您进行自学。
您首先需要的是IDE或集成开发环境。这是一个允许您编写和编辑 C 代码的文本编辑器的花哨术语。
一些最好的 C IDE 包括Visual Studio Code和Netbeans。这些是易于下载和设置的直观IDE 。(IDEs)毕竟,你的重点应该放在代码上——而不是学习特定编辑工具的细微差别。
这些工具还可以轻松下载并在系统上设置必要的编译器,以便您可以在编写代码后对其进行测试。
编写程序
一旦您拥有了用 C 编写代码所需的基本工具,您就可以着手编写您的第一个程序。C 语言程序包含三个基本元素。第一个是库(Library),它是头文件的集合。您需要将库导入程序中才能使用其中的功能。
对于此示例,必要的库是<stdio.h>。无论标题如何,所有 C 库都将以.h 结尾。(.h)要在代码中包含库,您将输入#include <stdio.h>
还在迷茫吗?如果您有使用Java编码的经验,请将其视为公共课程。
代码的下一部分是函数。(Function.)在 C(以及其他语言)中,函数是一组执行任务的语句。所有 C 程序中存在的主要函数是main()。这是代码:
int main() {
printf(“Hello, world!”);
返回0;(return 0;)
}
函数main()(main())前面的int命令(int)表明它将在完成时返回一个整数。在此之后,printf()命令是<stdio.h>库的一部分。如果不在此代码的开头调用库,printf()命令将不会运行。printf()命令中的文本(“ Hello , world!”)将显示在屏幕上。
一旦此函数运行,它将向程序返回一个 0。这是退出语句,基本上可以说程序已经完成了它的任务。main()后的左括号和右括号并返回 0;(return 0;)包含里面的功能。
返回0(return 0;);是程序的最后一部分。它表明代码中概述的任务已经结束。请记住(Bear),函数中的每一行都必须以分号结尾。这是语言语法的一部分。整个程序放在一起应该是这样的:
#include <stdio.h>
int main() {
printf (“Hello, world!”);
返回0;(return 0;)
}
如果它看起来令人困惑,但不要担心。学习 C 的特定命令可能会有点令人困惑,但经过一些练习后,您将完全没有问题。
C 实践的其他资源
如果您刚开始使用 C,您将需要大量练习编码。您对语言的语法和机制的实践越多,它就会变得越容易。这些是网络上一些用于学习如何(resources on the web for learning how to code)用 C编写代码的最佳免费资源。
- 学习-C.org(Learn-C.org)
该网站包含许多针对初学者的交互式 C 教程,您可以一次完成一个。您无需注册或下载任何内容;所有处理和编译都在网站本身内处理。它从最基本的命令开始用户并从那里构建。
- CProgramming.com
CProgramming.com相当于 C 编程的入门级大学课程。它提供了有关 C 编程语言的非常详细的信息,包括如何阅读语句、设置递归程序,甚至如何理解二叉树。
- W3Schools 教程(W3Schools Tutorial)
W3Schools教程系列是学习几乎任何编程语言的最佳资源之一。无论是 C、Java还是更晦涩的东西,您都可以在此处找到信息。W3Schools课程涵盖了大量有关 C 的信息,并包括实践编程测试,以将分数带回家 。
C Tutorial For Beginners: It’s Easier Than You Think!
There are few programming languagеs with the versatilіty and utility of С, but the lаnguage itself can often seem daunting, especially to a newcomer. The good news is that C isn’t as difficult to learn as it may seem, and there are numerous resources available for free to help you get your feet wet.
The History of C Programming
C programming was developed in 1972 by Dennis M. Ritchie. Several languages have since branched off from the core C language, including C++ and Objective C. The language was created as a general-purpose language to be used in a wide variety of applications, and it has met that goal.
In fact, C is one of the most widely used programming languages in existence, if not the most widely used. Why C, you might ask? It succeeded a previous programming language called B. In the modern world, C is used for system programming more than software programming.
Why Learn C?
Many newcomers to the programming world learn Java or Python first. These are some of the most popular modern languages, but C has just as much utility. Amateur coders are often surprised to find that C is easy to learn due to its structure. It’s capable of producing efficient, streamlined programs and can handle lower-level activities better than other languages.
Perhaps the biggest strength of C is that it can be compiled on a variety of platforms. In fact, Unix was written entirely in C.
The Basics of C Programming
Before we continue further with this C tutorial for beginners, know this: A programming language is easy to learn, but difficult to master. There are so many elements that influence how a language works that it’s impossible for a single article to cover even the most basic aspects. This guide will help you find the resources you need and teach you the core concepts so that you can self-educate.
The first thing you’ll need is an IDE, or an integrated development environment. This is a fancy term for a text editor that allows you to write and edit C code.
A few of the best IDEs for C include Visual Studio Code and Netbeans. These are intuitive IDEs that are easy to download and set up. After all, your focus should be on the code — not learning the nuances of a specific editing tool.
These tools also make it easy to download and set up the necessary compilers on your system so that you can test your code once it’s written.
Writing a Program
Once you have the basic tools you need to code in C, you can embark on writing your first program. There are three basic elements to a program in C. The first is the Library, which is a collection of header files. You’ll need to import a library into the program in order to use the functions within it.
For this example, the necessary library is <stdio.h>. All C libraries will end in .h, regardless of the title. To include a library within the code, you’ll enter #include <stdio.h>
Still confused? If you have experience coding in Java, think of it like a public class.
The next part of the code is the Function. In C (as well as other languages), a function is a group of statements that perform a task. The primary function present in all C programs is main(). Here’s the code:
int main() {
printf(“Hello, world!”);
return 0;
}
The int command in front of the function main() shows that it will return an integer when finished. Following this, the printf() command is part of the <stdio.h> library. Without calling the library at the start of this code, the printf() command won’t run. The text within the printf() command (“Hello, world!”) is what will be displayed on the screen.
Once this function runs, it will return a 0 to the program. This is the exit statement, and basically serves to say that the program has completed its task. The opening and closing brackets after main() and return 0; contain the function within.
The return 0; is the final part of the program. It indicates that the tasks outlined within the code have come to an end. Bear in mind that every line within the function has to end in a semicolon. This is part of the language’s syntax. The entire program put together should look like this:
#include <stdio.h>
int main() {
printf (“Hello, world!”);
return 0;
}
If it looks confusing, but don’t worry. Learning the specific commands for C can be a little confusing, but after a bit of practice you’ll have no trouble at all.
Additional Resources For C Practice
If you’re just starting out with C, you’ll want to get a lot of practice coding. The more hands-on you are with the syntax and the mechanics of the language, the easier it will become. These are some of the best free resources on the web for learning how to code in C.
- Learn-C.org
This website includes a number of interactive C tutorials for beginners that you can work through one at a time. You don’t have to sign up or download anything; all processing and compiling is handled within the website itself. It starts users with the most basic commands and builds from there.
- CProgramming.com
CProgramming.com is the equivalent of an entry-level college course for C programming. It provides highly detailed information regarding the C programming language, including how to read statements, set up recursive programs, and even how to understand binary trees.
- W3Schools Tutorial
The W3Schools tutorial series is one of the best resources for learning nearly any programming language, period. Whether it’s C, Java, or something more obscure, you can find information here. The W3Schools course covers a huge amount of information about C and includes practice programming tests to drive the points home.