下面是一些关于数据类型和与数据类型相关联的修饰符的逻辑和有趣的事实:
1.如果没有给变量指定数据类型,编译器会自动将其转换为int数据类型。
#include <stdio.h> int main() { signed a; signed b; // size of a and b is equal to the size of int printf("The size of a is %d\n", sizeof(a)); printf("The size of b is %d", sizeof(b)); return (0); }
输出:
The size of a is 4 The size of b is 4
2.signed是char和int数据类型的默认修饰符。
#include <stdio.h> int main() { int x; char y; x = -1; y = -2; printf("x is %d and y is %d", x, y); }
输出:
x is -1 and y is -2.
3.我们不能在float数据类型中使用任何修饰符。如果程序员试图使用它,编译器会自动给出编译时错误。
#include <stdio.h> int main() { signed float a; short float b; return (0); }
输出:
[Error] both 'signed' and 'float' in declaration specifiers [Error] both 'short' and 'float' in declaration specifiers
4.double数据类型中只允许long修饰符。我们不能为double数据类型使用其他说明符。如果我们尝试其他说明符,编译器编译时将出现错误。
#include <stdio.h> int main() { long double a; return (0); }
#include <stdio.h> int main() { short double a; signed double b; return (0); }
输出:
[Error] both 'short' and 'double' in declaration specifiers [Error] both 'signed' and 'double' in declaration specifiers
[1]Bishal Kumar Dubey.Interesting facts about data-types and modifiers in C/C++[EB/OL].https://www.geeksforgeeks.org/interesting-facts-about-data-types-and-modifiers-in-c-cpp/,2020-04-17.