C 与 C++ 的关系C++ 继承了所有的 C 特性C++ 在 C 的基础上提供了更多的语法和特性C++ 的设计目标是运行效率与开发效率的统一C 到 C++ 的升级C++ 更强调语言的实用性所有的变量都可以在需要使用时再定义int c = 0;for(int i=0; i<=3; i++){ for(int j=0; j<=3; j++) { c += i * j; }}对比:C 语言中的变量都必须在作用域开始的位置定义 【C89】register 关键字请求编译器将局部变量存储于寄存器中在 C++ 中依然支持 register 关键字C++ 编译器由自己的优化方式C 语言中无法获取 register 变量的地址C++ 中可以取得 register 变量的地址C++ 编译器发现程序中需要取 register 变量的地址时,register 对变量的声明变得无效早期 C 语言编译器不会对代码进行优化,因此 register 变量是一个很好的补充。在 C 语言中,重复定义多个同名的全局变量是合法的在 C++ 中,不允许定义多个同名的全局变量C 语言中多个同名的全局变量最终会被链接到全局数据区的同一个地址空间上实例分析: C 到 C++ 的升级 一Test_1.cppTest_1.c#include <stdio.h>int main(int argc, char* argv[]){ printf(“Begin …\n”); int c = 0; for(int i=0; i<=3; i++) { for(int j=0; j<=3; j++) { c += i * j; } } printf(“c = %d\n”, c); register int a = 0; printf("&a = %p\n", &a); printf(“End …\n”); return 0;}g++ 输出:【无警告,无错误】Begin …c = 36&a = 0xbfec9910End …gcc 输出: test.c:9: error: ‘for’ loop initial declarations are only allowed in C99 modetest.c:9: note: use option -std=c99 or -std=gnu99 to compile your codetest.c:11: error: ‘for’ loop initial declarations are only allowed in C99 modetest.c:21: error: address of register variable ‘a’ requestedTest_2.cppTest_2.c#include <stdio.h>int g_v;int g_v;int main(int argc, char* argv[]){ printf(“g_v = %d\n”, g_v); return 0;}g++输出:test.c:4: error: redefinition of ‘int g_v’test.c:3: error: ‘int g_v’ previously declared heregcc输出:g_v = 0struct 关键字的加强C 语言中的 struct 定义了一组变量的集合C 语言中 struct 定义的标识符并不是一种新的类型C++ 中的 struct 用于定义一个全新的类型typedef struct _tag_student Student;struct _tag_student{ const char* name; int age;}; <==> C 和 C++ 中结构体的等价定义struct Student{ const char* name; int age;}面试中的小问题:int f() 与 int f(void) 有区别吗?如果有区别是什么?C++ 中所有的标识符都必须显示的声明类型C 语言中的默认类型在 C++ 中是不合法的f(i){ printf("%d\n", i);}g(){ return 5;}问题:函数 f 的返回值和参数分别是什么类型?函数 g 可以接受多少个参数?在 C 语言中int f() 表示返回值为 int, 接受任意参数的函数f(void) 表示返回值为 int 的无参函数在 C++ 中int f() 和 int f(void) 具有相同的意义表示返回值为 int 的无参函数实例分析: C 到 C++ 的升级Test_3.cppTest_3.c#include <stdio.h>struct Student{ const char* name; int age;};int main(int argc, char* argv[]){ Student s1 = {“D.T.”, 30}; Student s2 = {“Software”, 30}; return 0;}g++输出:无错误,无警告gcc输出:test.c:11: error: ‘Student’ undeclared (first use in this function)test.c:11: error: (Each undeclared identifier is reported only oncetest.c:11: error: for each function it appears in.)test.c:11: error: expected ‘;’ before ‘s1’test.c:12: error: expected ‘;’ before ‘s2’Test_4.cppTest_4.c#include <stdio.h>f(i){ printf("%d\n", i);}g(){ return 5;}int main(int argc, char* argv[]){ f(10); printf(“g() = %d\n”, g(1, 2, 3, 4, 5)); return 0;}g++输出:test.c:3: error: expected constructor, destructor, or type conversion before ‘(’ tokentest.c:8: error: ISO C++ forbids declaration of ‘g’ with no typetest.c: In function ‘int main(int, char**)’:test.c:15: error: ‘f’ was not declared in this scopetest.c:8: error: too many arguments to function ‘int g()’test.c:17: error: at this point in filegcc输出:无错误,无警告10g() = 5小结C++ 更强调实用性,可以在任意的地方声明变量C++ 中的 register 只是一个兼容作用C++ 编译器能够更好的进行优化C++ 中的任意标识符都必须显示的指明类型以上内容参考狄泰软件学院系列课程,请大家保护原创!