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
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);
}
The size of y is 4
int main()
{
int x;
char y;
x = -1;
y = -2;
printf("x is %d and y is %d", x, y);
}
{
signed float a;
short float b;
return (0);
}
[Error] both 'short' and 'float' in declaration specifiers
int main()
{
long double a;
return (0);
}
#include<stdio.h>
int main()
{
short double a;
signed double b;
return (0);
}
[Error] both 'signed' and 'double' in declaration specifiers
No comments:
Post a Comment
Thank you for your Time. Keep Learning