Saturday, 23 December 2017

Facts about Data-types and Modifiers in C/C++ - Geeks4Coding

Data Types and Modifiers in C/C++

Data Types

They are used to define type of variables and contents used. Data types define the way you use storage in the programs you write. Data types can be built in or abstract.

Built in Data Types: These are the data types which are predefined and are wired directly into the compiler. eg: int, char etc.

User defined or Abstract data types: These are the type, that user creates as a class. In C++ these are classes where as in C it was implemented by structures. 

Modifiers

Specifiers modify the meanings of the predefined built-in data types and expand them to a much larger set. There are four data type modifiers in C++, they are :

  • long
  • short
  • signed
  • unsigned

Below mentioned are some important points you must know about the modifiers,

  • long and short modify the maximum and minimum values that a data type will hold.
  • A plain int must have a minimum size of short.
  • Size hierarchy : short int < int < long int 
  • Size hierarchy for floating point numbers is : float < double < long double 
  • long float is not a legal type and there are no short floating point numbers. 
  • Signed types includes both positive and negative numbers and is the default type. 
  • Unsigned, numbers are always without any sign, that is always positive. 

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, then the compiler automatically converted it to int data type.

#include <stdio.h>
int main()
{
signed x;
signed y;

// size of x and y is equal to the size of int
printf("The size of x is %d\n", sizeof(x));
printf("The size of y is %d", sizeof(y));
return (0);
}

 

Output:
 

The size of x is 4
The size of y is 4

 

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

#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 float data type. If programmer try to use it then compiler automatically give compile time error.
 

#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 the long modifier is allowed in double data types. we cant use any other specifier with double data type. If we try any other specifier then compiler will give compile time error.
 

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


#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

No comments:

Post a Comment

Thank you for your Time. Keep Learning

Geeks4Coding

Contributors

Popular Posts