//difference ①: Usability Enhancement#include <iostream>using namespacestd;The variables in the//C language must be defined at the beginning of the scope!! More emphasis is placed on the "practicality" of language in C + +, and all variables can be defined when they need to be used. voidMain () {intA =1; printf ("ddddd"); intKk//This definition kk,c language will be in the VC6.0 error, in vs2013 not, but C + + supports this definitionSystem"Pause");}
//difference ②:register keyword Enhancement (c + + version)#include <iostream>using namespacestd;//Register keyword Request compiler let the variable a directly inside the register, fast//Register-Modified variables in C are not addresses, but in C + + content//Change of register keyword//Register keyword Request "compiler" to store local variables in registers//cannot get register variable address in C language//Register keyword is still supported in C + +//the C + + compiler has its own optimization method, and it may be optimized without register//Address of register variable can be obtained in C + +//when the C + + compiler discovers that the address of a register variable is required in a program, the register of the variable becomes invalid. //The early C language compiler does not optimize the code, so the register variable is a good addition. voidMain () {//In C + +, if a variable appears multiple times or is used frequently, the C + + compiler automatically uses register for this variable to optimize//the i,c++ compiler here automatically optimizes I with the Register//Register int i = 0; inti =0; for(i =0; I <3; i++) { ; } printf ("the address of the Register modifier variable is%x\n", &i); //when the C + + compiler discovers that the address of a register variable is required in a program, the register of the variable becomes invalid. //at this point the C + + compiler will handle the I variable so that the register of the variable I becomes invalidSystem"Pause");}
//difference ②:register keyword Enhancement (c language version)#include <stdio.h>#include<stdlib.h>#include<string.h>voidMain () {registerinti =0; for(i =0; I <3; i++) { ; } printf ("the address of the Register modifier variable is%x\n", &i);//Print error C2103: "&" on register variable//Register keyword requests the compiler to store local variables in registers, which of course do not take the address of memory//so the register variable address cannot be obtained in C languageSystem ("Pause");}
// difference ③: global variable Detection enhancement (c + + version) #include <iostream>using STD; // // c++ directly rejects the practice of ambiguity. int G_var; int G_var; int G_var = 1 void Main () {System ( " pause );}
//difference ③: Global variable Detection enhancement (C language version)#include <stdio.h>#include<stdlib.h>#include<string.h>//in the C language, it is legal to repeatedly define multiple global variables with the same name//multiple global variables with the same name in the C language will eventually be linked to the same address space on the global data areaintG_var;intG_var;intG_var=1;voidMain () {System ("Pause");}
The difference between C + + C + + and language first