• [其他] 关于C语言中使用未声明函数的问题


    在c语言中,碰到一个.c文件,无.h头文件,在另一.c文件调用函数时,并没有进行声明extern,

    此时编译器不会报错,会默认去查找同名的函数,这样会存在一些问题,查了些资料,稍微总结了下:

    总结:

    1.声明函数可以不加extern,函数默认extern。
    2.声明变量必须要加extern.
    3.如果不加extern,编译器会默认去查找同函数名的函数,但会出错。
      1).参数中如果出现float,一定会出现读取错误,但编译运行不报错.如果形参float后有int*类型的变量,编译运行会报错,之前有则不会报错。
      2).函数形参如果都是整数类型的值(char、short、int、long、long long等),都可以正常传值,值的大小可以大于int范围
      3).函数返回值无所谓什么类型,但大小范围不能超过int型范围(-2147483648 ~ +2147483647),超过会返回值错误.
        可以理解为:默认返回值为int,后被强转成需要返回的类型(long、float等).

    main.c

    #include<stdio.h>
    //extern int test_1(int a);
    //int test_2(int a, int b);
    //...
    int main()
    {
        int a0 = 1;
        int a[10] = { 0 };
        int*b = a;
        float c = 0.01f;
        char d = 5;
        long long e = 2147483648;
        long long f = 2147483649;
    
        int a1 = test_1(a0);       //3
        int a2 = test_2(a0, a0 + 1);//3
        float a3 = test_3(a0, a0 + 1);//3.00
        float a4 = test_4(a0, c);// -85899342, c在那边读取2.000,
        int a5 = test_5(a0, c); //  3, c在那边读取2.000,
        int a6 = test_6(a0, c, a0 + 1);// 1065646818,  c在那边读取2.000, a0+1读为:1065646817 
        int a7 = test_7(a0, d); //6
        int a8 = test_8(a0, b); //3, b[1]正常写入8
        int a9 = test_9(a0, b, c);//3, c在那边读取2.000,b[1]正常写入10
        //int a10 = test_10(a0, c, b);//报错,c读取为2.00,b报内存冲突
        long long a11 = test_11(a0, e);//2147483647, e在那边正常读为2147483648,说明形参超出int读取正常
        long long a12 = test_11(a0, f);//-2147483648, f在那边正常读为2147483649,说明返回值超出int会读取出错
    
        getchar();
    }

    test.c

    int test_1(int a)
    {
        return a + 2;
    }
    int test_2(int a, int b)
    {
        return a + b;
    }
    float test_3(int a, int b)
    {
        return a + b;
    }
    float test_4(int a, float b)
    {
        float c = a + b;
        return c;
    }
    int test_5(int a, float b)
    {
        int c = a + b;
        return c;
    }
    int test_6(int a, float b, int c)
    {
        int d = a + c;
        return d;
    }
    int test_7(int a, char b)
    {
        int c = a + b;
        return c;
    }
    int test_8(int a, int* c)
    {
        c[1] = 8;
        return a + 2;
    }
    int test_9(int a, int* c, float b)
    {
        c[1] = 10;
        return a + 2;
    }
    int test_10(int a, float b, int* c)
    {
        c[1] = 10;
        return a + 2;
    }
    
    long long test_11(int a, long long b)
    {
        long long c =  b-1;
        return c;
    }

     C语言数据类型范围:http://blog.csdn.net/abaloon/article/details/8173552

    (32位系统)

    char -128 ~ +127 (1 Byte)
    short -32767 ~ + 32768 (2 Bytes)
    unsigned short 0 ~ 65536 (2 Bytes)
    int -2147483648 ~ +2147483647 (4 Bytes)
    unsigned int 0 ~ 4294967295 (4 Bytes)
    long == int
    long long -9223372036854775808 ~ +9223372036854775807 (8 Bytes)
    double 1.7 * 10^308 (8 Bytes)

    unsigned int 0~4294967295 
    long long的最大值:9223372036854775807
    long long的最小值:-9223372036854775808
    unsigned long long的最大值:1844674407370955161

    __int64的最大值:9223372036854775807
    __int64的最小值:-9223372036854775808
    unsigned __int64的最大值:18446744073709551615

  • 相关阅读:
    Piggy-Bank (hdoj1114)
    Word Amalgamation(hdoj1113)
    Lowest Bit(hdoj1196)
    1206: B.求和
    1207: C.LU的困惑
    STL初步
    关于521(nyoj)
    first blood暴力搜索,剪枝是关键
    变态最大值(nyoj)
    烧饼(nyoj779)
  • 原文地址:https://www.cnblogs.com/zwh0214/p/6693814.html
Copyright © 2020-2023  润新知