Data Types in C

Data Types in C Language

In this article, I am going to discuss Data Types in C Language with examples. Please read our previous article, where we discussed the Variables in the C Program. As part of this article, you will learn what are Data types in C, it’s type, and when and how to use Data Types in C Program with examples.

Data types are used to store data temporarily in the computer through the program. In the real world, we have different types of data like integer, floating-point, character, string, etc. To store all these types of data in the program to perform business required calculations and validations we need the concept data types.

Data Types in C Language

A data type in a programming language is a set of data values having predefined characteristics. Data Types refers to the kind of information that can be stored in memory.

The data type is something that specifies the size of the memory location, type of the data it can hold, and the minimum and maximum range of value that an object can accommodate. Here, object means, it is a named region of storage. It might be a variable or array or structure instance.

C data types are defined as the data storage format that a variable can store data to perform a specific operation. Data types are used to define a variable before to use in a program. The size of variable, constant, and array are determined by data types.

Data Types in C Language

C – DATA TYPES:

Data types are something which gives information about

  1. Size of the memory location.
  2. Range of data that can be stored inside that memory location
  3. Possible legal operations can be performed on that memory location.
  4. What types of results come out from an expression when these types are used inside that expression.

The keyword which gives all this information is called the data type. There are four data types in C language.

  • Basic data types or Fundamental or Built-in data types (Primary Data Types).
  • Derived data type (Secondary Data Types).
  • Enumeration data type.
  • Void data type.
Why the data types are classified into so many types?

The basic advantages of classifying these many types of nothing but utilizing the memory more efficiently and increasing the performance.

Built-in data types:

The basic built-in or fundamental data types that are available in C are:

  1. Int
  2. Float
  3. Double
  4. Char
Int data type

Int is used to define integer numbers. The size of the data type ‘int’ is 2 bytes or 16 bits. The minimum value for the signed ‘int’ data type is -32768. The maximum value for the signed ‘int’ data type is 32767. We can declare int data type as follows:

int c;
c=5;

Float data type

Float is used to define floating-point numbers. The size of the data type ‘float’ is 4 bytes or 32 bits. The minimum and maximum value for the ‘float’ data type are 3.4E-38 to 3.4E+38. We can declare a float data type as follows:

float Miles;
Miles=5.6;

double data type

Double is used to define BIG floating-point numbers. It reserves twice the storage for the number. On PCs, this is likely to be 8 bytes. The size of data type double is 4 bytes or 32 bits. The minimum and maximum value for the ‘double’ data type are 1.7E-308 to 1.7E+308. We can declare a double data type as follows:

double a;
a=2500;

char data type

Char defines characters. The size of data type char is 1 byte or 8 bits. The minimum and maximum value for the ‘char’ data type are -128 to 127. We can declare char data type as follows:

char Name;
Name=’x’;

Program:

#include <stdio.h>
int main()
{
    int a = 4000; 		// positive integer data type
    float b = 5.2324; 		// float data type
    char c = 'Z'; 		// char data type
    long d = 41657; 		// long positive integer data type
    long e = -21556; 		// long -ve integer data type
    int f = -185; 		// -ve integer data type
    short g = 130; 		// short +ve integer data type
    short h = -130; 		// short -ve integer data type
    double i = 4.1234567890; 	// double float data type
    float j = -3.55; 		// float data type
}
MODIFIERS IN C LANGUAGE:

The amount of memory space to be allocated for a variable is derived by modifiers. Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.

Example: storage space for the int data type is 4 byte for a 32-bit processor. We can increase the range by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.

There are 5 modifiers available in C language. They are,

  1. Short
  2. Long
  3. Signed
  4. Unsigned
  5. long

MODIFIERS IN C LANGUAGE

Derived data types:

Derived data types in C Programming Language are those C data types which are derived from the fundamental data types using some declaration operators. The basic derived types that are available in C are:

  1. Array.
  2. Functions
  3. Pointers.
  4. Structures.
  5. Classes.
Array derived data type

The arrays can be defined as a set of finite and homogeneous data elements. Each element of an array is referenced using an index.

Example: If the name of an array is A which have 4 elements then the array will be represented as :

A[0], A[1], A[2], A[3]. Here, these subscripts which are containing the digit is known as an index.

Function derived data type

The function can be defined as a part of the program which is declared by the programmer in any part of the program and a function can be of any name depending upon the choice of the programmer. The function declared can be invoked from other parts of the program.

Pointer derived data type

A pointer is a variable that holds the address of the memory space. If one variable can hold the address of another variable then it is said that the first variable is pointing to the second.

Structure derived data type

The structure can be defined as a collection or a group of variables that are referenced under one name. It is used to keep related information together. We use a ‘struct’ keyword to construct a structure.

Class derived data type

The class can be defined as a group of objects which are similar or of the same kind. The keyword ‘class’ is used to construct a class. The Class almost follows the same declaration procedure as we did in case of structures, but here comes the concept of access specifiers which applies a kind of restriction on the data. There are three types of access specifiers namely public, protected, and private.

Enumeration data type

Enumeration data type consists of named integer constants as a list. It starts with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list. Enum data type is a user-defined data type having a finite set of enumeration constants. The keyword ‘enum’ is used to create an enumerated data type.

Syntax: enum [data type] {const1, const2…., const n};

PROGRAM:

#include <stdio.h>
int main()
{
   enum MONTH 
   {
      Jan = 0, Feb, Mar 
   };
   enum MONTH month = Mar;
   if(month == 0)
   printf("Value of Jan");
   else if(month == 1)
   printf("Month is Feb");
   if(month == 2)
   printf("Month is Mar");
}

OUTPUT:

Data Types in C

Void Data Type:

The void is an empty data type that has no value and no operations. This can be used in functions and pointers. It’s a data type that represents the lack of a data type. Many programming languages need a data type to define the lack of return value to indicate that nothing is being returned.

Example: void f(void);

void f(); (accepts a constant but unknown number of arguments)

Uses of Void Data type:

When used as a function return type: the void keyword specifies that the function does not return a value.

void show()
{
      printf("This function has no return type");
}

When used for a function’s parameter list: void specifies that the function takes no parameters.

int sum(void)
{
      int a,b;
      printf("Enter Two number>> ");
      scanf("%d%d",&a,&b);
      return a+b;
}

When used in the declaration of a pointer: void specifies that the pointer is “universal.”

void main()
{
     void *p;
     int a=10;
     char b='A';
     float c=9.19;
     p=&a;
     printf("\nPrinting Integer data %d",(*(int *)p));
     p=&b;
     printf("\nPrinting character data %c",(*(char *)p));
     p=&c;
     printf("\nPrinting float data %f",(*(float *)p));
}
What data types when to use in C?
  • In implementation when we are required character operations then go for ‘char’ or ‘unsigned char’ data type.
  • For normal numeric operations go for “int” data type. If there is no -ve representation then go for “unsigned int” data type like employee salary.
  • In implementation when we required the numeric values for the range of -128 to 127 then go for “char” data type in place of creating an “int” in this case we required to use the %d format specifier.
  • In implementation when we required the numeric values from the range of “0 to 255” then go for an unsigned char data type in place of creating unsigned int. In this case, we required to use the”%u” format specifier.
  • Signed, unsigned, short and long are called qualifier and we need to apply these qualifiers to an integral type only i.e. we cannot apply to float, double, and long double type.
  • Signed, unsigned is called signed qualifiers.
  • Short and long are called size qualifiers.
  • By default any integral variable size is short and the sign is signed type.

In the next article, I am going to discuss Type Casting in C with examples. Here, in this article, I try to explain Data Types in C Language in detail with examples and I hope you enjoy this Data Types in C Language article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *