《A Tour of C++ Third Edition》1. The Basics

1.1 Introduction

本章讲的大多是C中的语言特性,也就是过程式编程的特性,包括表记、内存模型、计算以及组织代码的方式

1.2 Programs

C++代码到程序的编译过程

同一个可执行程序在不同操作系统上是不可以运行的,由于文件系统的原因,exe文件在Linux上无法运行等。当我们讨论可移植性时,我们讨论的是souce code

C++标准定义了语言中两种实体:

  1. 核心语言特性,包括char、int等内置类型和循环等特性
  2. 标准库组件,包括容器、IO操作等

C++是静态类型语言,编译期所有实体的静态类型都需要确定,因为类型决定了操作和内存占用

1.2.1 Hello, World!

int main() { } // the minimal C++ program

这是最短的一段C++代码

C++代码以main函数作为程序起点(当然除了main还有其他的额外部分),main函数的返回值会被操作系统利用(指Linux),main函数返回非0值时代表出错

import std;
int main() {
	std::cout << "Hello, World!\n";
}

<<将右边参数写入左边参数

import std; // import the declarations for the standard library
using namespace std; // make names from std visible without std:: (§3.3)
double square(double x) // square a double-precision floating-point number
{
	return x * x;
}
void print_square(double x) {
	cout << "the square of " << x << " is " << square(x) << "\n";
}
int main() {
	print_square(1.234); // print: the square of 1.234 is 1.52276
}

面向过程的风格通常是把功能写在函数里,然后由main函数直接或间接调用

1.3 Functions

讲了函数的组成部分和重载,后面还会详细介绍

void print(int,double);
void print(double,int);
void user2()
{
    print(0,0); // error: ambiguous
}

1.4 Types, Variables, and Arithmetic

讲的是类型系统,其他都是编程语言讲烂的东西,主要说一下C++里的坑以及C++新时代的改进

1.4.2 Initialization

double d1 = 2.3; // initialize d1 to 2.3
double d2 {2.3}; // initialize d2 to 2.3
double d3 = {2.3}; // initialize d3 to 2.3 (the = is optional with { ... })
complex<double> z = 1; // a complex number with double-precision floating-point scalars
complex<double> z2 {d1,d2};
complex<double> z3 = {d1,d2}; // the = is optional with { ... }
vector<int> v {1, 2, 3, 4, 5, 6}; // a vector of ints

大括号是更统一的初始化方式,且遇到损失精度的收窄转换narrowing conversions时会在编译阶段报错

auto d = 1.2; // a double
auto z = sqrt(y); // z has the type of whatever sqrt(y) returns
auto bb {true}; // bb is a bool

auto 一般用于复杂类型的对象声明

1.5 Scope and Lifetime

这也是很重要的概念:作用域与生存期

先讲作用域:

  1. 局部作用域:函数内部或lambda表达式内部的对象
  2. 类作用域:类成员或枚举成员
  3. 命名空间作用域:命名空间中函数、lambda、类、枚举类型之外的对象

这里强调全局对象的作用域是全局作用域global namespace 

vector<int> vec; // vec is global (a global vector of integers)
void fct(int arg) // fct is global (names a global function)
// arg is local (names an integer argument)
{
    string motto {"Who dares wins"}; // motto is local
    auto p = new Record{"Hume"}; // p points to an unnamed Record (created by new)
    // ...
}

struct Record {
    string name; // name is a member of Record (a string member)
    // ...
};

再看生存期:

  1. 局部对象:使用前被构造,在作用域尾部被销毁(全局对象则在程序结束时销毁)
  2. 类成员:类对象被销毁时成员一起销毁
  3. 动态(匿名)对象:new时被创建,delete时被销毁 

1.6 Constants

不可变性主要有两种含义:

  • const:‘‘I promise not to change this value.’’

const关键字主要用于修饰接口,这样通过指针或引用传入的参数将不可被改变,const对象的求值可能在运行时

  • constexpr:‘‘to be evaluated at compile time.’’ 

constexpr关键字主要修饰常量,允许数据存放在ROM,且追求性能时使用,常量表达式的值必须在编译期可求

constexpr int dmv = 17; // dmv is a named constant
int var = 17; // var is not a constant
const double sqv = sqrt(var); // sqv is a named constant, possibly computed at run time

double sum(const vector<double>&); // sum will not modify its argument (§1.7)

vector<double> v {1.2, 3.4, 4.5}; // v is not a constant
const double s1 = sum(v); // OK: sum(v) is evaluated at run time
constexpr double s2 = sum(v); // error: sum(v) is not a constant expression

函数想要用在常量表达式中的话,必须声明为constexpr或consteval

constexpr double square(double x) { return x*x; }

constexpr double max1 = 1.4*square(17); // OK: 1.4*square(17) is a constant expression
constexpr double max2 = 1.4*square(var); // error: var is not a constant, so square(var) is not a constant

const double max3 = 1.4*square(var); // OK: may be evaluated at run time

constexpr函数可以传入非常量参数,但此时结果将不再是constexpr

当我们只想要函数在编译期求值而非要求它是常量表达式时,使用consteval

consteval double square2(double x) { return x*x; }
constexpr double max1 = 1.4*square2(17); // OK: 1.4*square(17) is a constant expression
const double max3 = 1.4*square2(var); // error: var is not a constant

书上把这两类函数称为pure functions,意思是没有副作用且不能使用函数外的非局部变量

这两类函数的用处主要在:

  1. 常量表达式:数组下标等语言规则、case标签、模板参数、常量声明
  2. 编译期求值对性能有提升

1.7 Pointers, Arrays, and References

T a[n] // T[n]: a is an array of n Ts
T* p // T*: p is a pointer to T
T& r // T&: r is a reference to T
T f(A) // T(A): f is a function taking an argument of type A returning a result of type T

1.7.1 The Null Pointer

int count_x(const char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
    if (p==nullptr)
        return 0;
    int count = 0;
    for (; *p!=0; ++p)
        if (*p==x)
            ++count;
    return count;
}

 这里提到指针放在条件判断处的应用,如果指针不是nullptr,则if(p)为true

1.8 Tests

void do_something(vector<int>& v)
{
    if (auto n = v.size(); n!=0) {
        // ... we get here if n!=0 ...
    }
    // ...
}

 if语句中也可以定义局部变量

这里n!=0其实都可以省略

1.9 Mapping to Hardware

1.9.2 Initialization

Initialization differs from assignment. In general, for an assignment to work correctly, the
assigned-to object must have a value. On the other hand, the task of initialization is to make an
uninitialized piece of memory into a valid object. For almost all types, the effect of reading from or
writing to an uninitialized variable is undefined. 

这里说初始化和赋值不是一件事,初始化是保证对象在使用前分配内存,通过给定初始值的方式使它成为内存中的对象;而赋值是对已有的值进行修改

  • 13
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
This book is devoted to practical C programming. C is currently the premier language for software developers. That's because it's widely distributed and standard. Newer languages are available, such as C++, but these are still evolving. C is still the language of choice for robust, portable programming. This book emphasizes the skills you will need to do real-world programming. It teaches you not only the mechanics of the C language, but the entire life cycle of a C program as well (including the program's conception, design, code, methods, debugging, release, documentation, maintenance, and revision). Good style is emphasized. To create a good program you must do more than just type in code. It is an art in which writing and programming skills blend themselves together to form a masterpiece. True art can be created. A well-written program not only functions correctly, but is simple and easy to understand. Comments allow the programmer to include descriptive text inside the program. When clearly written, a commented program is highly prized. A program should be as simple as possible. A programmer should avoid clever tricks. This book stresses simple, practical rules. For example, there are 15 operator precedence rules in C. These can be simplified into two rules: 1. Multiply and divide come before add and subtract. 2. Put parentheses around everything else. Consider two programs. One was written by a clever programmer using all the tricks. The program contains no comments, but it works. The other program is well commented and nicely structured, but it doesn't work. Which program is more useful? In the long run, the broken one. It can be fixed. Although the clever program works now, sooner or later all programs have to be modified. The worst thing that you will ever have to do is to modify a cleverly written program. This handbook is written for people with no previous programming experience or programmers who already know C and want to improve their style and reliability. You should have access to a computer and TEAM FLY PRESENTS 9 know how to use the basic functions such as a text editor and the filesystem. Specific instructions are given for producing and running programs using the UNIX operating system with a generic cc compiler or the Free Software Foundation's gcc compiler. For MS-DOS/Windows users, instructions are included for Borland C++, Turbo C++, and Microsoft Visual C++. (These compilers compile both C and C++ code.) The book also gives examples of using the programming utility make for automated program production. How This Book is Organized You must crawl before you walk. In Part I we teach you how to crawl. These chapters enable you to write very simple programs. We start with the mechanics of programming and programming style. Next, you learn how to use variables and very simple decision and control statements. In Chapter 7, we take you on a complete tour of the software life cycle to show you how real programs are created. Part II describes all of the other simple statements and operators that are used in programming. You'll also learn how to organize these statements into simple functions. In Part III we take our basic declarations and statements and learn how they can be used in the construction of advanced types such as structures, unions, and classes. We'll also introduce the concept of pointers. Finally, a number of miscellaneous features are described Part IV. Chapter
### 回答1: 《C语言入门之旅:基础知识》是一本以C语言为主题的书籍,旨在帮助读者快速掌握C语言的基本知识。以下是对该书的回答: 《C语言入门之旅:基础知识》这本书重点介绍了C语言的基本概念、语法特性以及常见用法。它适合那些想要学习C语言编程,但没有任何编程经验的读者。该书从简单易懂的角度出发,循序渐进地引导读者入门C语言。 首先,《C语言入门之旅:基础知识》详细介绍了C语言的起源和发展历程,帮助读者了解C语言的背景和重要性。接着,书中从最基本的数据类型开始,逐步介绍了C语言的各类数据类型、变量和常量的定义与使用方法。读者通过实例代码的学习和练习,可以更好地理解这些概念。 该书还介绍了C语言的运算符和表达式,包括算术运算符、关系运算符、逻辑运算符等。通过这部分的学习,读者能够掌握基本的运算和表达式求值的方法。 另外,书中还涵盖了C语言的流程控制语句,例如条件语句(if-else语句)、循环语句(for循环、while循环等)和跳转语句(break语句、continue语句等)。读者可以学习如何使用这些语句来实现程序的控制流。 此外,该书还介绍了C语言中的数组和字符串,以及如何使用它们进行数据的存储和处理。同时,它还包含了C语言的函数创建与调用、指针的使用方法等重要内容。 总之,《C语言入门之旅:基础知识》通过简单易懂的语言,系统全面地介绍了C语言的基础知识。无论是初学者还是有一定编程基础的读者,都可以通过这本书快速入门C语言编程,打下坚实的基础。 ### 回答2: " C语言基础之旅" 是一次让您了解C语言基础知识的旅程。C语言是一种广泛应用于系统编程和嵌入式设备的高级编程语言,也是许多计算机科学专业学习中的必修课程。 首先,我们将介绍C语言的历史和起源,以及它为什么成为一种如此重要的编程语言。接着,我们将深入了解C语言的基本语法和语义,包括变量、数据类型、运算符、控制结构和函数定义等。这些基础知识将为您构建和理解C语言程序打下坚实的基础。 在旅程的下一站,我们将探索C语言的输入和输出,包括如何从键盘接收输入和将数据输出到屏幕或文件中。我们还将介绍如何使用C语言的库函数来进行字符串处理、数学运算和内存管理等。 然后,我们将进一步深入研究C语言的数组和指针,这是C语言中一个非常重要也最具挑战性的概念。我们将学习如何声明、初始化和操作数组,以及如何使用指针来访问和操作内存中的数据。这些概念将帮助我们提高代码的效率和灵活性。 在最后的旅程中,我们将介绍C语言的结构体和文件操作。通过学习如何定义和使用结构体,我们可以创建自定义的数据类型来组织和管理数据。而文件操作将使我们能够读取和写入文件,使我们的程序能够与外部系统进行交互。 通过这次"C语言基础之旅",您将全面了解C语言的基础知识和概念,为进一步深入学习编程和开发复杂的应用程序奠定坚实的基础。无论是寻求职业发展还是追求计算机科学的真正爱好者,掌握C语言将成为您的重要优势。让我们一起开始这次旅程吧! ### 回答3: 《C语言基础教程》是一本针对初学者的C语言入门指南。C语言是一种通用的编程语言,广泛应用于软件程序的开发。该书通过一个实例化的游览项目,带领读者逐步了解C语言的基本概念和技巧。 首先,该书介绍了C语言的历史和发展,并解释了为什么学习C语言对于软件开发人员至关重要。它探讨了C语言与其他编程语言的区别,以及C的主要特征和优势。 接下来,书中详细解释了如何设置C语言开发环境。读者将学会如何安装和配置C语言的编译器,以及如何使用集成开发环境(IDE)进行编程。此外,该书还介绍了用于编写C代码的文本编辑器和调试器,并提供了一些常见问题的解决方案。 在了解了开发环境之后,书中引导读者学习C语言的基本语法和语义。读者将了解如何声明和使用变量、编写基本的控制结构(如条件语句和循环),以及如何定义和调用函数。通过大量的示例代码和练习题,读者可以逐步巩固所学内容。 此外,《C语言基础教程》还介绍了C语言中常用的数据类型、运算符和数组等。读者将学会如何使用这些概念来解决实际问题,并掌握一些常见的编程技巧和最佳实践。 尽管《C语言基础教程》只是C语言学习的一个入门指南,但它提供了足够的基础知识,让读者能够理解和编写简单的C程序。通过学习这本书,读者可以为进一步深入学习C语言奠定坚实的基础,并为未来的软件开发之旅做好准备。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

+xiaowenhao+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值