[C/CPP系列知识] Type difference of character literals 和 bool in C and C++
生活随笔
收集整理的這篇文章主要介紹了
[C/CPP系列知识] Type difference of character literals 和 bool in C and C++
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C/C+中的每一個常亮(every literal)都是有類型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a(chǎn)’)在C和C++中有不同的變量類型。
在C中,‘a(chǎn)’被認(rèn)為是int形,在C++中,‘a(chǎn)’被認(rèn)為是char型。
int main() {printf("sizeof('V') = %d sizeof(char) = %d", sizeof('V'), sizeof(char));return 0; }結(jié)果:
C result –?sizeof(‘V’) = 4 sizeof(char) = 1
C++ result –?sizeof(‘V’) = 1 sizeof(char) = 1
上述行為也可以體現(xiàn)C++的函數(shù)重載
void foo(char c) {printf("From foo: char"); } void foo(int i) {printf("From foo: int"); }int main() {foo('V');return 0; }?
則調(diào)用void foo(char c)
?
?
3)?Types of boolean results are different in C and C++.?
// output = 4 in C (which is size of int) printf("%d", sizeof(1==1)); // output = 1 in c++ (which is the size of boolean datatype) cout << sizeof(1==1);?
總結(jié)
以上是生活随笔為你收集整理的[C/CPP系列知识] Type difference of character literals 和 bool in C and C++的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样保证客户端和服务器端数据的一致性(数
- 下一篇: [C++]VisualAssistX中文