C++编译器与C的几点差异

#include "stdlib.h"
#include "stdio.h"
#include "string.h"


int main()
{

    const int a=100;
    int *p;

    printf("a:%d\n", a);//打印a=100

    //C++对const取&会分配内存
    p=(int *)&a;
    *p=10;
    printf("*p:%d\n", *p);//打印*p=10
    printf("a:%d\n", a);//打印a=100

    return 0;
}

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

int main()
{
    int a = 10;
    int b = 20;
    int c = 0;

    //返回一个最小数 并且给最小数赋值成3
    //三目运算符是一个表达式 ,一般情况表达式不可能做左值
    //若让三目表达式表达式做左值,b=30
    (a > b ?  a : b ) =  30;
    //花式写法,c=1
    c =((a > b ?  a : 1 ));
    printf("a = %d, b = %d ,c = %d,\n", a, b, c);

     return 0;
}

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

void fun1()
{ 
    //#define为宏替换,由预编译器处理,若无#undef,之后的地方都可发生替换
    #define a 10;
    //const为定义常量,由编译器处理,会进行域检查等操作
    const int b = 20;

    //#undef
}

void fun2()
{
     printf("a = %d\n", a);<--不报错

     printf("b = %d\n", b);<--报错
}

int main(int argc, char *argv[])
{
    fun1();
    fun2();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少言才不会咸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值