📜  有关C / C++中的数据类型和修饰符的有趣事实

📅  最后修改于: 2021-05-25 23:48:42             🧑  作者: Mango

以下是有关数据类型和与数据类型相关联的修饰符的一些逻辑和有趣的事实:-

1.如果没有为变量提供数据类型,则编译器会自动将其转换为int数据类型。

C++
#include 
using namespace std;
  
int main()
{
    signed a;
    signed b;
  
    // size of a and b is equal to the size of int
    cout << "The size of a is " << sizeof(a) <


C
#include 
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);
}


C++
#include 
using namespace std;
  
int main()
{
    int x;
    char y;
    x = -1;
    y = -2;
    cout << "x is "<< x <<" and y is " << y << endl;
}
  
// This code is contributed by shubhamsingh10


C
#include 
int main()
{
    int x;
    char y;
    x = -1;
    y = -2;
    printf("x is %d and y is %d", x, y);
}


C++
#include 
using namespace std;
int main()
{
    signed float a;
    short float b;
    return (0);
}
//This article is contributed by shivanisinghss2110


C
#include 
int main()
{
    signed float a;
    short float b;
    return (0);
}


C++
#include 
using namespace std;
int main()
{
    long double a;
    return (0);
}
  
// This code is contributed by shubhamsingh10


C
#include 
int main()
{
    long double a;
    return (0);
}


C++
#include 
using namespace std;
int main()
{
    short double a;
    signed double b;
    return (0);
}
  
// This code is contributed by shubhamsingh10


C
#include 
int main()
{
    short double a;
    signed double b;
    return (0);
}


输出:

The size of a is 4
The size of b is 4

2.带符号是char和int数据类型的默认修饰符。

C++

#include 
using namespace std;
  
int main()
{
    int x;
    char y;
    x = -1;
    y = -2;
    cout << "x is "<< x <<" and y is " << y << endl;
}
  
// This code is contributed by shubhamsingh10

C

#include 
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数据类型中使用任何修饰符。如果程序员尝试使用它,编译器会自动给出编译时错误。

C++

#include 
using namespace std;
int main()
{
    signed float a;
    short float b;
    return (0);
}
//This article is contributed by shivanisinghss2110

C

#include 
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.双重数据类型中仅允许使用long修饰符。我们不能将其他说明符与double数据类型一起使用。如果尝试其他说明符,编译器将给出编译时错误。

C++

#include 
using namespace std;
int main()
{
    long double a;
    return (0);
}
  
// This code is contributed by shubhamsingh10

C

#include 
int main()
{
    long double a;
    return (0);
}

C++

#include 
using namespace std;
int main()
{
    short double a;
    signed double b;
    return (0);
}
  
// This code is contributed by shubhamsingh10

C

#include 
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
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。