C++与C区别随笔记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhao3132453/article/details/82857051

1、全局变量

#include <stdio.h>

int a=100;
int a;

void main()
{
    printf("%d\n",a);
}

//C语言中以上写法是可以的

#include <iostream>
using namespace std;

int a=100;
int a;

void main()
{
    cout<<a<<endl;
}

////C++语言中以上写法是不可以的

2、struct

#include <stdio.h>

struct TT
{
    int age;
};

void main()
{
    struct TT t;
}

//C语言中定义结构体对象struct不能省略

#include <iostream>

using namespace std;

struct TT
{
    int age;
};

void main()
{
    TT t;
}

//C++语言中定义结构体对象struct可以省略

3、表达式

#include <stdio.h>

void main()
{
    int a,b;
    (a>b?a:b)=20;
}

//C语言中三目表达式返回的是一个数,不能作为左值

#include <iostream>

using namespace std;

void main()
{
   int a=1,b=2;
   (a>b?a:b)=100;
}

//C++语言中三目表达式返回的是一个变量,能作为左值

4、const

#include <stdio.h>

void main()
{
    const int a=100;
    int *p=NULL;
    p=&a;
    *p=200;
}

//C语言中const的值会被修改

#include <iostream>

using namespace std;

void main()
{
   const int a=100;
    int *p=NULL;
    p=&a;
    *p=200;
}

//C++语言中const的值不会被修改

猜你喜欢

转载自blog.csdn.net/zhao3132453/article/details/82857051
今日推荐