C与C++的区别,枚举类型enum

Thinking in c++ p98


//enum.c

#include <stdio.h>

enum ShapeType{
    circle = 10, square = 20, rectangle = 30
};

int main(){

    enum ShapeType shape = circle;
    shape++;
    printf("%d\n", shape);

    return 0;
}

amrzs@ubuntu:cc$ gcc enum.c
amrzs@ubuntu:cc$ ./a.out 
11


//enum.cpp

#include <cstdio>
using namespace std;

enum ShapeType{
    circle = 10, square = 20, rectangle = 30
};

int main(){

    ShapeType shape = circle;
    shape++;
    printf("%d\n", shape);

    return 0;
}
amrzs@ubuntu:cc$ g++ enum.cpp 
enum.cpp: In function ‘int main()’:
enum.cpp:11:10: error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]
     shape++;

这点可以看出,C++并不是C的超集,把C++看做另外一门语言更好,因为C++与C有很多兼容的语法,使得C程序员更容易入手C++。

但总的来说,写法标准的C程序还是可以用C++编译的,只是C++的编译器语法检查更加严格。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值