C 2 C 到 C 的公升級

2021-09-13 02:17:23 字數 2930 閱讀 1540

}對比:c 語言中的變數都必須在作用域開始的位置定義 【c89】

c++ 編譯器發現程式中需要取 register 變數的位址時,register 對變數的宣告變得無效

早期 c 語言編譯器不會對**進行優化,因此 register 變數是乙個很好的補充。

test_1.cpp

test_1.c

#include int main(int argc, char* ar**)

}printf("c = %d\n", c);

register int a = 0;

printf("&a = %p\n", &a);

printf("end ...\n");

return 0;

}

g++ 輸出:【無警告,無錯誤】

begin ...

c = 36

&a = 0xbfec9910

end ...

gcc 輸出:

test.c:9: error: 『for』 loop initial declarations are only allowed in c99 mode

test.c:9: note: use option -std=c99 or -std=gnu99 to compile your code

test.c:11: error: 『for』 loop initial declarations are only allowed in c99 mode

test.c:21: error: address of register variable 『a』 requested

test_2.cpp

test_2.c

#include int g_v;

int g_v;

int main(int argc, char* ar**)

g++輸出:

test.c:4: error: redefinition of 『int g_v』

test.c:3: error: 『int g_v』 previously declared here

gcc輸出:

g_v = 0

typedef struct _tag_student student;

struct _tag_student

;

<==> c 和 c++ 中結構體的等價定義

struct student

面試中的小問題:int f() 與 int f(void) 有區別嗎?如果有區別是什麼?

f(i)

g()

問題:

函式 f 的返回值和引數分別是什麼型別?

函式 g 可以接受多少個引數?

在 c++ 中

test_3.cpp

test_3.c

#include struct student

;int main(int argc, char* ar**)

; student s2 = ;

return 0;

}

g++輸出:

無錯誤,無警告

gcc輸出:

test.c:11: error: 『student』 undeclared (first use in this function)

test.c:11: error: (each undeclared identifier is reported only once

test.c:11: error: expected 『;』 before 『s1』

test.c:12: error: expected 『;』 before 『s2』

test_4.cpp

test_4.c

#include f(i)

g()int main(int argc, char* ar**)

g++輸出:

test.c:3: error: expected constructor, destructor, or type conversion before 『(』 token

test.c:8: error: iso c++ forbids declaration of 『g』 with no type

test.c: in function 『int main(int, char**)』:

test.c:15: error: 『f』 was not declared in this scope

test.c:8: error: too many arguments to function 『int g()』

test.c:17: error: at this point in file

gcc輸出:無錯誤,無警告

10g() = 5

C到C 的公升級 (2)

c 在c語言的基本型別系統之上增加了bool。c 中的bool可取的值只有true和false,這兩個是c 中的關鍵字。理論上bool只占用乙個位元組,如果多個bool變數定義在一起,可能會各佔乙個bit,這取決於編譯器的實現。true代表真值,編譯器內部用1來表示。false代表非真值,編譯器內部...

C2C平台的組織架構

c2c平台的組織架構也就是整個c2c平台的組成,乙個大的c2c平台的執行離不開很多小的部門相互協調合作,c2c平台的整個執行包括大到上級對貨源的調配,小到每乙個客服對客戶的解答,其中任何乙個環節都是缺一不可的。今天我們就來 一下,c2c平台的組織架構 我們建造乙個屬於自己的c2c平台,首先我們需要擁...

lessons 2 c到c 的公升級

register關鍵字請求編譯器將區域性變數儲存於暫存器中,c語言中無法獲得register變數的位址,但是c 中可以取得register變數的位址 函式定義與預設值 int fun fun int fun void fun void fun i 1 c語言中 fun 預設返回值為int型,形參為任...