Showing posts with label Data Types in C Programming. Show all posts
Showing posts with label Data Types in C Programming. Show all posts

Saturday, 23 December 2017

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

Data Types in C


All the variable in C has data type associated with it. Each variable require memory which depends on its data type and has some specific operations which can be performed over it. Let us briefly describe them one by one:

Following are the examples of some very common data types used in C:

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.

int: As the name suggests, an int variable is used to store an integer.

float: It is used to store decimal numbers (numbers with floating point value) with single precision.

double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Void: The type specifier void indicates that no value is available. This data type will be discussed in pointers section. Click here

Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.

We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:

#include <stdio.h>
int main()
{
int a = 1;
char b ='G';
double c = 3.14;
printf("Hello World!\n");
 

printf("%c and %lu byte.\n", b,sizeof(char));

printf("%d and %lu bytes.\n", a,sizeof(int));

printf("%lf and %lu bytes.\n",c,sizeof(double));

return 0;
}


Output:

Hello World!

G and 1 byte.
1 and 4 bytes.
3.140000 and 8 bytes.

Geeks4Coding

Contributors

Popular Posts