C和C++中的一些问题

本文探讨了在C和C++中结构体的使用差异,指出在C++中可以直接像类一样声明结构体变量,而在C语言中则需要使用typedef定义类型别名。
摘要由CSDN通过智能技术生成

C和C++中的一些问题

1. C语言是否可以进行重载?

测试代码如下:
    #include <stdio.h>
    #include <stdlib.h>

    int f(int a, char b)
    {
        return a;
    }

    float f(int a, float b)
    {
        return b;
    }

    char f(char a)
    {
        return a;
    }

    int main()
    {
        int a;
        float b;
        char c;
        f(a, c);
        f(a,b);
        f(c);

        return 0;
    }

    从测试代码可以看出,其中包含了重载的基本情况:参数的个数不同和参数的类型不同。

    使用不同的编译器进行测试:
    1. CodeBlocks 8.02中,建立C的工程。
    
    ||=== test, Debug ===|
    \test\main.c|10|error: conflicting types for 'f'|
    \test\main.c|5|error: previous definition of 'f' was here|
    \test\main.c|15|error: conflicting types for 'f'|
    \test\main.c|10|error: previous definition of 'f' was here|
    \test\main.c|15|error: conflicting types for 'f'|
    \test\main.c|10|error: previous definition of 'f' was here|    
    \test\main.c||In function `main':|
    \test\main.c|24|error: too many arguments to function `f'|
    \test\main.c|25|error: too many arguments to function `f'|
    ||=== Build finished: 8 errors, 0 warnings ===|

    编译时使用 mingw32-gcc进行C代码编译。

    2. C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值