Open In App

Interesting facts about data-types and modifiers in C/C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Here are some logical and interesting facts about data-types and the modifiers associated with data-types:- 

1. If no data type is given to a variable, the compiler automatically converts it to int data type. 

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


Output:

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

2. Signed is the default modifier for char and int data types. 

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


Output:

x is -1 and y is -2

3. We can’t use any modifiers in the float data type. If the programmer tries to use it, the compiler automatically gives compile time error. 

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


Output:

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

4. Only long modifier is allowed in double data types. We can’t use any other specifier with double data type. If we try any other specifier, compiler will give compile time error. 

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


Output:

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

 Explanation:

  • C and C++ have a rich set of data types, including integer types (such as char, short, int, and long), floating-point types (such as float and double), and others (such as void and enum).
  • In C and C++, data type sizes are not guaranteed to be the same on all platforms. For example, int might be 16 bits on one platform and 32 bits on another. To ensure your code is portable, you can use types defined in the standard headers such as <stdint.h>.
  • C and C++ have several type modifiers, such as signed, unsigned, and short, which can be used to modify the range and precision of integer types. For example, using unsigned int instead of int will give you an unsigned integer type with a larger range of non-negative values.
  • The long type modifier can be used to increase the size of an integer type. For example, long int is a larger integer type than int, and long long int is a larger integer type than long int.
  • In C++, bool is a built-in data type that represents a boolean value (i.e., either true or false). In C, bool is not a built-in type, but it can be defined as an int or an enum.
  • In C and C++, the const type modifier can be used to declare a constant value. For example, const int x = 42; declares a constant integer value x with a value of 42.
  • C and C++ also support user-defined data types, such as structures and classes, which allow you to define complex data structures with multiple members.


Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads