Home > Backend Development > C#.Net Tutorial > What are the differences between C/C++? A comparison method that many people don't know about

What are the differences between C/C++? A comparison method that many people don't know about

php是最好的语言
Release: 2018-07-24 17:31:41
Original
2234 people have browsed it

C/C should be compared from the number of keywords, source files, variable definitions or declaration locations, functions, and default parameters. If you are always confused, reading this article will help you. .

C/C Comparison from the following aspects:

  1. Number of keywords:
    C language: C99 version, 32 keywords
    C: C98 version, 63 keywords

  2. Source file:
    C source file suffix .c, C source file suffix .cpp, if nothing is given when creating the source file, the default is .cpp

  3. Variable definition or declaration location:
    C language must be defined on the first line; C is not required

  4. Function:
    (1) Return value
    In C language , if a function does not specify a return value type, it returns int type by default;
    In C, the detection of function return values ​​is more stringent. If a function does not return a value, it must be specified as void.
    (2) Parameters List
    In the C language, if a function does not specify a parameter list, it can accept any number of parameters by default; but in C, due to strict parameter type detection, functions without a parameter list default to void and do not accept any parameters. .

  5. Default parameters:
    Default parameters specify a default value for the parameters of the function when declaring or defining the function. When calling this function, if no actual parameters are specified, the default value is used; otherwise, the specified actual parameters are used.

//1.实现缺省参数void Test(int a = 50){
    cout << a << endl;
}
int main(){    Test();    // 输出50
    Test(100); // 输出100}
Copy after login


(1) All default parameters: Give all default values ​​​​for all parameters //Code

// 实现全缺省参数void Test(int a = 1,int b = 2,int c = 3)
{    cout << a << "" <<" "<< b << "" <<" "<< c << endl; 
}int main()
{
    Test();//1 2 3
    Test(100);//100 2 3
    Test(100, 200);//100 200 3
    Test(100, 200, 300);//100 200 300}
Copy after login

(2) Semi-default parameters: It is stipulated that the default value can only be passed from right to left //Code

// 实现半缺省参数   注:缺省值只能从右往左传void Test1(int a = 1, int b = 2, int c = 3)
{    cout << a << "" << " " << b << "" << " " << c << endl;
}void Test2(int a , int b = 2, int c = 3)
{    cout << a << "" << " " << b << "" << " " << c << endl;
}void Test3(int a , int b , int c = 3)
{    cout << a << "" << " " << b << "" << " " << c << endl;
}void Test4(int a = 1, int b , int c = 3)//不能通过编译,因为它违背了缺省值只能从右往左依次来给这一规定{    cout << a << "" << " " << b << "" << " " << c << endl;
}void Test5(int a = 1, int b = 2, int c )//不能通过编译,因为它违背了缺省值只能从右往左依次来给这一规定{    cout << a << "" << " " << b << "" << " " << c << endl;
}void Test6(int a = 1, int b , int c )//不能通过编译,因为它违背了缺省值只能从右往左依次来给这一规定{    cout << a << "" << " " << b << "" << " " << c << endl;
}void Test7(int a , int b = 2, int c )//不能通过编译,因为它违背了缺省值只能从右往左依次来给这一规定{    cout << a << "" << " " << b << "" << " " << c << endl;
}int main()
{
    Test1();//1 2 3}
Copy after login

Note:

a. 带缺省值的参数必须放在参数列表的最后面。
b. 缺省参数不能同时在函数声明和定义中出现,只能二者则其一,最好放在函数声明中。
c. 缺省值必须是常量或全局变量。
Copy after login

C language does not support default parameters

Function overloading

  • Function overloading refers to declaring several functions of the same name with similar functions in the same scope. The formal parameter lists of these functions with the same name (each number, type, order of types) must be different.

//函数重载void Add();void Add(int a);//行参个数不一样void Add(char b);//行参类型不同void Add(int a, char b);void Add(char a, int b);//行参类型的次序不同
Copy after login
  • Just the type of the return value is different, which cannot constitute function overloading.

//仅仅返回值的类型不同,是不能构成函数重载的void Add(int a, int b)
{}int Add(int a, int b)
{    return a + b;
}int main()
{
    Add(1, 2);//因为这样会造成调用不明确,两函数都可以被调用
    return 0;
}
Copy after login
  • C The reason why function overloading is supported: VS editor compiles the type of function parameters into the name of the function at the bottom, so the original function name is Changed to another unique name.

int Add(int a, int b);    // ?Add@@YAHHH@Zchar Add(int a, int b);   // ?Add@@YADHH@Zchar Add(char a, char b); // ?Add@@YADDD@Z
Copy after login
  • The reason why C language does not support function overloading: the new function name generated is still the same. Just add _

  • in front of the function name. To compile the function in C language style, just add extern “c”

    ## in front of the function name.
  • #
    extern "C" int Add(char a, int b);
    Copy after login
Quote

There are two ways to pass parameters to functions in C language:

Passing by value and Passing by address

Passing by value: During the function call, a temporary variable will be generated and replaced by a formal parameter. Finally, the value of the actual parameter will be passed to the newly allocated temporary variable, that is, the formal parameter.
Advantages of passing by value: The side effects of the function will not affect the external parameters.
Disadvantages of passing by value: External parameters cannot be changed by modifying parameters.

Passing reference: During the function call, a temporary variable will be generated and replaced with formal parameters, and finally the address of the actual parameter will be passed to the newly allocated temporary variable.
Advantages of passing references: space saving, high efficiency, changing parameters can change external actual parameters.
Disadvantages of passing pointers: Pointers are unsafe, and the side effects of functions will affect external actual parameters.

C Medium:

Reference: (1) Concept: Reference is not to newly define a variable, but to an existing variable After taking an alias, the compiler will not allocate memory space for the reference variable. It shares the same memory space with its reference variable.
(2) Form:
type & reference variable name = reference entity

//引用int main()
{    int a = 10;    int& ra = a;    printf("%p\n", a);    printf("%p\n", ra);//ra和a的地址相同,说明ra和a是同一个实体,他们共用同一块内存空间

    ra = 3;    printf("%d\n", a);//3
    return 0;
}
Copy after login

Note:

   a. 引用在定义时,必须初始化。
   b. 一个变量可以被多次引用。
   c. 引用一旦引用了一个实体,就不能在引用其他实体。
   d. 引用变量的生命周期比实体的生命周期短。
Copy after login

(3) Frequent reference

常引用int main()
{    const int a = 1;    //int& ra = a;//编译会出错,因为实体a是常量
    const int& ra = a;    double b = 12.34;    //int& rb = b;//编译会出错,因为类型不同
    const int& rb = b;    printf("rb=%d\n", rb);//rb=12
    b = 5.0;    printf("b=%f\n", b);//b=5.0
    printf("rb=%d\n", rb);//rb=12
    //b的值改变,但rb的值并没有随之改变,说明rb和b是两个不同的实体}
Copy after login

(4) Array reference

//数组引用int a[10];//数组a的类型为 int[10]int(&ra)[10] = a;
Copy after login

(5) Reference scenario:

a. Use references as parameters of functions to change actual parameters.

void Swap(int* pLeft, int* pRight)
{    int temp = *pLeft;    *pLeft = *pRight;    *pRight = temp;
}

void Swap(int& left, int& right)
{    int temp = left;
    left = right;
    right = temp;
}
//如果用引用时不想改变实参的值,则给引用前加const
void Swap(const int& left, const int& right);int main()
{    int a = 10;    int b = 20;
    Swap(&a, &b);//通过传地址来改变实参    printf(" a=%d ", a);    printf(" b=%d\n", b);

    Swap(a, b);//通过引用来改变实参    printf(" a=%d ", a);    printf(" b=%d\n", b);
}
Copy after login

b. Use reference variables as the return value of the function //Code

情形1:int& FunTest()
{    int a = 10;    return a;
}int main()
{    int b = FunTest();//将函数的返回值赋给了b
    printf("b=%d\n", b);//b=10
    printf("b=%d\n", b);//b=10
    printf("b=%d\n", b);//b=10
    return 0;
}

情形2:int& FunTest2()
{    int a = 10;    return a;
}int main()
{    int& b=FunTest2();//将函数的返回值作为实体,
    printf("b=%d\n", b);//b=10
    printf("b=%d\n", b);//随机值
    printf("b=%d\n", b);//随机值
    return 0;
}

情形3:int& FunTest3(int& a)
{
    a = 10;    return a;
}int main()
{    int b;    int& rb = FunTest3(b);    printf("b=%d\n", b);//b=10
    printf("rb=%d\n", rb);//rb=10
    printf("rb=%d\n", rb);//rb=10
    printf("rb=%d\n", rb);//rb=10
    return 0;
}
注意:不能返回栈空间上的引用
Copy after login

Comparison of the efficiency of passing by value, passing by reference, and reference

//比较struct BigType
{    int array[10000];
};void FunTest(BigType bt)//传值或传址{}void FunTest(BigType& bt)//引用{}void TestFunTestRumTime()
{
    BigType bt;
    size_t Start = GetTickCount();    for (i = 0; i < 1000000; i++)
    {
        FunTest(bt);//传值或传引用
        FunTest(&bt);//传址
    }
    size_t End = GetTickCount();    printf("%d\n", End - Start);
}//此代码检测出传值最慢,而传址和引用速度快且用时差不多相同
Copy after login

There are two differences between references and pointers What's the difference?

Similar points:

  • List content

  • The underlying processing method is the same, both based on pointers realized.

  • The type of pointer corresponding to the reference variable at the bottom layer:

  • The type of the reference variable entity* const

Differences:

  • References must be initialized; pointers are not required.

  • A pointer of an ordinary type can point to any object of the same type at any time; once a reference refers to an entity, it cannot refer to other entities.

  • Pointer: points to the next address; reference: gives a value.

  • has different meanings in sizeof: the reference result is the size of the reference type; and the pointer is always the address * the number of bytes occupied by the space.

  • Pointers need to be addressed manually; references are addressed by the compiler.

  • 引用比指针使用起来相对安全。

命名空间

在C++中,变量、函数和类都是大量存在的,这些变量、函数和类的名称将都存在于全局命名空间中,会导致很多冲突,使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染。

  • 命名空间的定义

//命名空间namespace N1
{    int a = 30;    void FunTest()
    {        printf("N1::FunTest()\n");
    }
}//N1的命名空间int a = 20;void FunTest()
{    printf("::FunTest()\n");
}//在全局作用域中int main()
{    int a = 10;    printf("%d\n", a);    printf("%d\n", ::a);
    ::FunTest();    printf("%d\n", N1::a);
    N1::FunTest();    return 0;
}//命名空间的嵌套namespace N2
{    int a = 40;    void FunTest()
    {        printf("N2::FunTest()\n");
    }    namespace N3
    {        int a = 50;        void FunTest()
        {            printf("N2::N3::FunTest()\n");
        }
    }
}int main()
{
    N2::FunTest();
    N2::N3::FunTest();    return 0;
}// 在同一个工程里允许存在多个相同名称的命名空间,编译器最后会合成到同一个命名空间中namespace N1
{    int b = 70;    void Test()
    {        printf("N1::Test()\n");
    }
}
Copy after login
  • 说明
    a.一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中。
    b.没有名称的命名空间只能在当前文件中使用,它里面定义的变量相当于工程里面的全局变量。

  • 命名空间的使用

//命名空间的使用namespace N1
{    int a = 1;    int b = 2;    int c = 3;    /*void FunTest1()
    {}
    void FunTest2()
    {}*/}//法二:using N1::b;//法三:using namespace N1;int main()
{    int a = 4;    //法一:
    printf("a=%d\n", N1::a);//a=1

    printf("b=%d\n", b);//b=2
    printf("c=%d\n", c);//c=3}
Copy after login

C++输入输出:

//代码

//C++输入输出#include <iostream>using namespace std;//std标准命名空间int main()
{    int a = 10;    double b = 3.14;    char c = &#39;c&#39;;    cout << a ;    cout << b << &#39;\n&#39;;    cout << c << endl;    cout << a << " " << b << " " << c << endl;    cin >> a ;    cin >> b >> c;    return 0;
}// cout:标准命名空间重输出流对象  <<输出操作符   // cin:标准命名空间重输入流对象   >>输入操作符
Copy after login

相关推荐:

C# 中的 == 和 equals()有什么区别  

C# 中的 == 和 equals()区别       

视频:C++视频教程

The above is the detailed content of What are the differences between C/C++? A comparison method that many people don't know about. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template