以下是关于数据类型和与数据类型相关的修饰符的一些合乎逻辑且有趣的事实:-
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.我们不能在浮点数据类型中使用任何修饰符。如果程序员尝试使用它,编译器会自动给出编译时错误。
#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 修饰符。我们不能使用任何其他具有双数据类型的说明符。如果我们尝试任何其他说明符,编译器将给出编译时错误。
#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