在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