(C/C++学习笔记) 一. 基础知识

一. 基础知识

● 程序和C/C++

程序: 根据Wirth (1976), Algorithms + Data Structures = Programs.

 

Whence C: 1972, Dennis Ritchie[ˈritʃi](丹尼斯·里奇), 贝尔实验室(Bell Labs), C语言是他和Ken Thompson在设计Unix操作系统时创建的, Ritchie因此被称为C语言之父和Unix之父.

 

Whence C++: 1983-1985, Bjarne Stroustrup([ˈbjɑːnə ˈsdʁʌʊ̯ˀsdʁɔb]本贾尼·斯特劳斯特卢普), 贝尔实验室(Bell Labs), C++C的扩展, 最初被称为带类的C(C with class).

 

● 计算机系统的组成

 

● 信息存储单位 unit of storage

位(比特:bitb,小写):度量数据的最小单位.

字节(ByteB,大写):最常用的基本单位,1字节=8

* 一个英文字母,无论大写和小写都是一个字符(character)=一个字节=8位。

一个汉字是一个字符=两个字节=16位。

 

K (千)字节(Kilobyte            1K / 1KB / 1kB = 1024 Byte

M(兆)字节(Megabyte        1M = 1024 K

G(吉) 字节(Gigabyte        1G = 1024 M

T(太)字节(Terabyte        1T = 1024 G

 

 

C/C++程序的编译过程

.cpp文件是文本文件(text file), .obj.exe文件是二进制文件(binary file), lib是静态数据连接库文件

 

C/C++程序结构

  • 上面是一个C语言程序的案例; 还应注意以下几点, 在声明区上面经常有注释来说明程序的先关信息, 例如:

/***********************************

*    程序文件名: test.cpp                    *

*    本程序功能: 显示Hello World         *

*    设计者: Abelhinho                    *

*    时间: 12-25-2015                        *

***********************************/

 

main()函数的返回值

对于main()函数来讲,return后面的整数是告诉操作系统程序执行的结果,return 0程序正常结束;return -1(也可以是其它非0整数, 但一般是-1)表示程序结束时出错了.

大多数情况下main()函数的返回值我们是用不着的,但操作系统支持一个进程来获取别的进程的退出代码,这个时候这个返回值就有意义了。

例如,你在写一个自动安装程序,要依次安装多个应用程序,那么你怎么知道其中某个程序的安装是否正常结束了呢,因为如果某个出错了再继续也许是毫无意义的,你就可以通过别的进程的退出码来判断了。

 

※ 在 main()函数中,return (0)exit(EXIT_SUCCESS) exit(0) 基本上是等价的。

但是事实上 return 和直接调用 exit 的效果并不完全一样。一般情况下应使用 return() 来结束 main(),但如果 main() 中分配了内存,则应使用 exit() 结束.

一般来说,除非是多次分配或者系统的内存空间非常紧张,否则 main(),特别是短命程序如命令行工具的 main() 中分配的内存不应使用 free() 释放)。

 

● 输入Hello World!

#include <iostream>        //头文件引用

using namespace std;    //命名空间的引用; 如果写成<iostream.h>, 就不用写下面的 using namespace std; 不过, c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h, 虽然编译和运行都没问题

//另外, iostream是一个头文件,系统头文件不带.h后缀, 也就是说, 自定义的头文件要带.h后缀

int main()

{

    cout<<"Hello World!"<<endl;    //或者写成cout<<"Hello World! "

    return 0;

}

 

C/C++符号 C/C++ tokens

C/C++ tokens are of six types. They are:

1. Keywords (eg: int, while),

2. Identifiers (eg: main, total—reserved or self-defined),

3. Constants (eg: 10, 20),

4. Strings (eg: "total", "hello"),

5. Special symbols (eg: (), {}),

6. Operators (eg: +, /,-,*)

 

C++的关键字表

C++的关键字表

asm

const_cast

explicit

inline

public

struct

typename

auto

continue

export

int

register

switch

union

bool

default

extern

long

reinterpret_cast

template

unsigned

break

delete

false

mutable

return

this

using

case

do

float

namespace

short

throw

virtual

catch

double

for

new

signed

true

void

char

dynamic_cast

friend

operator

sizeof

try

volatile

class

else

goto

private

static

typedef

wchar_t

const

enum

if

protected

static_cast

typeid

while

 

● 自定义标识符(identifier)的要求

① 可以使用大小写字母, 下划线, 数字(其余的都不行,比如汉字、连字符hyphen、运算符+、-/, 但第一个字符必须是字母或者下划线(underline/underscore), 不过应避免第一个字符是下划线"_"或双下划线"__", 因为一些语言内部名称或预定义宏也是用"_""__", 从而造成命名冲突. 另外注意:

② 字母区分大小写;

③ 变量名最好用英文,而且要有所含义, 即通过变量的名称就能猜测变量的意思'

C语言中变量的首字母一般是小写的

linuxunix的风格都是小写字母,windows是小写大写结合;

⑥ 类名一般以大写C开头, CBook, CStudent

 

● 匈牙利命名法(Hungarian notation)

※ 之所以叫做匈牙利命名法是为了纪念匈牙利籍的 Microsoft 程序员 Charles Simonyi.

基本原则是:变量名=属性+类型前缀+对象描述

 

属性(要用时在类型前缀前加):

g_        全局变量

c_        常量

m_        c++类成员变量

s_        静态变量

 

类型前缀:

a        数组

p        指针

fn        函数

h        句柄

l        长整型

b        布尔

f        浮点型(有时也指文件)

dw        双字    //一个字为2个字节, 16, 简称WORD; 两个字为双字,4个字节, 32, 简称DWORD,

sz        以''结束的字符串

n        短整型

d        双精度浮点

c        (通常用cnt)计数

ch        (通常用c)字符

i        (通常用n)整型

by        字节

w        字

r        实型

u        无符号

lp        32位长整数指针

msg        消息

 

描述部分:

Max        最大

Min            最小

Init            初始化

T            (或Temp)临时变量

Src            源对象

Dest        目的对象

 

● 有关foo

术语foobar, foo, bar, baz qux经常在计算机编程或计算机相关的文档中被用作占位符的名字(placeholder name)。当变量,函数,或命令本身不太重要的时候,foobar, foo, bar, baz qux就被用来充当这些实体的名字,这样做的目的仅仅是阐述一个概念,说明一个想法。这些术语本身相对于使用的场景来说没有任何意义。Foobar经常被单独使用;而当需要多个实体举例的时候,foobar,和baz则经常被按顺序使用。

 

● 声明 declare & 定义 define

. 变量的声明有两种情况:

(1) 一种是需要建立存储空间的(定义、声明)。例如:int a;表示在声明的时候就已经建立了存储空间。

(2) 另一种是不需要建立存储空间的(声明)。例如:extern int a;其中变量a是在别的文件中定义的。

前者是"定义性声明(defining declaration)"或者称为"定义(definition)",而后者是"引用性声明(referncing declaration)"。从广义的角度来讲声明中包含着定义,但是并非所有的声明都是定义,例如:int a它既是声明,同时又是定义。然而对于extern int a; 来讲它只是声明不是定义。一般的情况下我们常常这样叙述,把建立空间的声明称之为"定义",而把不需要建立存储空间称之为"声明"。很明显我们在这里指的声明是狭义的,也就是说非定义性质的声明。

 

. 函数的定义与声明:

(1) 函数声明("函数原型"形式相同)是让编译器知道函数的返回值类型, 名称和形参等信息, 函数的声明在主函数之前;

※ 以前的C版本的函数声明方式不是采用函数原型,而只是声明函数名和函数类型。如:float add(); 不包括参数类型和参数个数。系统不检查参数类型和参数个数。新版本也兼容这种用法,但不提倡这种用法,因为它未进行全面的检查.

(2 ) 函数定义是让编译器知道函数的功能, 函数的定义在主函数之后;

(3) 这里的声明是狭义的, 我们也可以直接在主函数之前声明函数后直接定义函数

 

. 类的声明和定义:

(1) 类的声明是让编译器知道类的名称及其内部结构(数据成员)和接口(成员函数);

(2) 类的定义(又称类体的定义)指的是成员函数的定义.

(3) 在软件中, 类的声明一般在.h文件里, 类的定义一般在.cpp文件里

A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:

extern int bar;

extern int g(int, int);

double f(int, double); // extern can be omitted for function declarations

class foo; // no extern allowed for type declarations

A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;

int g(int lhs, int rhs) {return lhs*rhs;}

double f(int i, double d) {return i+d;}

class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C++:

double f(int, double);

double f(int, double);

extern double f(int, double); // the same as the two above

extern double f(int, double);

However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.

 

● 移位运算

#include<iostream>

using namespace std;

int main()

{

    int a=0x40,b,c;        //整型不一定是十进制的, 这里是十六进制的

    cout<<"a in decimal system is: "<<a<<dec<<endl;    //C++默认输出十进制, 这里的dec可以省略,     cout<<"a in decimal system is: "<<a<<endl;

    b=a<<1;        //左移1

    c=a>>1;        //右移1

    cout <<b<< endl;

cout <<c<< endl;

    return 0;

}

 

● 十进制转二进制

#include<iostream>

using namespace std;

int main()

{

    int a = 10;

    char binbuf[32]; //存储二进制字串的空间

    printf("%s ", itoa(a, binbuf,2)); //最后一个参数2表示2进制

}

 

#include<iostream>

using namespace std;

int main()

{

    int n;

    cout << "input n:";

    cin >> n;

    for(int i=1; i<=32; i++)

    {

        cout << (n<0?1:0) << (i%8==0?" ":"");

        n = n<<1;

    }

     cout << endl;        // 回车的意思, 相当于C语言里面的printf(" ")

     return 0;

}

 

原文地址:https://www.cnblogs.com/ArrozZhu/p/8377515.html