Datos interesantes sobre tipos de datos y modificadores en C/C++

Aquí hay algunos hechos lógicos e interesantes sobre los tipos de datos y los modificadores asociados con los tipos de datos:

1. Si no se proporciona ningún tipo de datos a una variable, el compilador la convierte automáticamente al tipo de datos int.

C++

#include <iostream>
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) <<endl; 
    cout << "The size of b is " << sizeof(b); 
    return (0);
}
  
// This code is contributed by shubhamsingh10

C

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

Producción:

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

2. Firmado es el modificador predeterminado para los tipos de datos char e int.

C++

#include <iostream>
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 <stdio.h>
int main()
{
    int x;
    char y;
    x = -1;
    y = -2;
    printf("x is %d and y is %d", x, y);
}

Producción:

x is -1 and y is -2.

3. No podemos usar ningún modificador en el tipo de datos flotante. Si el programador intenta usarlo, el compilador automáticamente da un error de tiempo de compilación.

C++

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

C

#include <stdio.h>
int main()
{
    signed float a;
    short float b;
    return (0);
}

Producción:

[Error] both 'signed' and 'float' in declaration specifiers
[Error] both 'short' and 'float' in declaration specifiers

4. Solo se permite el modificador largo en tipos de datos dobles. No podemos usar ningún otro especificador con tipo de datos doble. Si intentamos cualquier otro especificador, el compilador dará un error de tiempo de compilación.

C++

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

C

#include <stdio.h>
int main()
{
    long double a;
    return (0);
}

C++

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

C

#include <stdio.h>
int main()
{
    short double a;
    signed double b;
    return (0);
}

Producción:

[Error] both 'short' and 'double' in declaration specifiers
[Error] both 'signed' and 'double' in declaration specifiers

Este artículo es una contribución de Bishal Kumar Dubey . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *