Basic Data Types In C With Example

Data Types In C

What is Data Types?

Data Types usually defines a set of variables, which can be stored with the variable along with operation that may be performed on those values. Data type indicates the type of data, a variable can have.

In other words, Data types in c define the way in which values and range of values are represented in a system. C has a very small yet expressive set of data types. The data types it contains efficiently and effectively represent all kinds of data value.

It is following types:

  • Primary Data Type
  • Derived Data Type
  • User-defined Data Type

Basic data types table containing commonly used types size of data types, format specifiers, and data types ranges in C Programming Language:

Variable TypesKeywordSize (Bytes)FormatRange
Integer typeint2 byte%d-32768 to +32767
signed integer2 byte%hu-31768 to 32767
unsigned integer2 byte%a0 to 65535
Short integershort int2 byte%hd-31768 to 32767
signed short int2 byte%hd-32768 to 32767
unsigned short int2 byte%hu0 to 65535
Long integerLong int4 byte%li-2147483648 to 2147483647
signed long integer4 byte%ld-2147483648 to 2147483647
unsigned long integer4 byte%lu0 to 4294967295
Character typeChar1 byte%c-128 to 127
signed Character 1 byte%c-128 to 127
unsigned character1 byte%c0 to 255
Floating typefloat4 bytes%f3.4E-38 to +3.4E+38
Double typedouble8 bytes%lf1.7E-308 to 1.7E+308
Long double typelong10 bytes%Lf3.4E-4932 to 1.1E+4932

Primary Data Type in C:

The primary data type is used form representing a single value only. It is also called a simple or primitive data type. These are used at the primary level in the high-level language, so it is called primary or primitive data type.

These are categories into the following categories.

  1. Integer type
  2. Character type
  3. Float type
  4. Double type

Integer Type

Integer type means without the decimal point. These are signed or unsigned. It is short int, int, long int. They occupy one word of storage and since the word sizes of machine vary (typically 16 to 32 bit), the size of an integer that can be stored depends on the computer. The size of the int is 2 bytes that are 16 bits storage.
Integers are whole numbers without any fractional part. It provides some control over the range of numbers and storage space.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int a=2;
    printf("Value of a in integer type is: %d", a);
    return 0;
}
 

Output:

Value of a in integer type is: 2

Structure variable data type:

We can use the data types of variable declaration the same as a normal variable declaration such as integer, float, char.
When int value exceeds than we use long int data types to accommodate the entered value or result if we want to print the reverse number 54678 than resultant will be 87645, but it exceeds the limit or range of the integer then we use long int at the lace of int.

struct n
{
   long int a; 
   char[15];
}

We also used short int as well as signed or unsigned int data type with structure. “When we have to insert an integer whose between 128 to 127 then we use the short int data type with structure.
When we have to insert an integer whose range between 0 to 65535 then we use short unsigned integer with structure.
If we have to insert an integer whose range between -32678 to +32678 hen we use signed int with structure.

For Example:

struct n
{
signed int a;
unsigned int b;
short int c;
};

Float Data Type:

The float type is also called precision real or very small real numbers. It has at least one digit with a decimal point. The range of float in C is 4 bytes i.e. 32 bits.

#include <stdio.h>
#include <stdlib.h>
 
float main()
{
    float a= 5.99;
    printf("Value of a in float type is: %f", a);
    return 0;
}
 
Value of a in float type is: 5.990000

Character Data Type:

Character type has either a single character in a single inverted comma like ‘W’. A combination of characters called a string. The size of a character in C is 1-byte storage space.

#include <stdio.h>
#include <stdlib.h>
 
char main()
{
    char a= 'W';
    printf("Character stored in a is: %c", a);
    return 0;
}
 
Character stored in a is: W

Double Data Type:

These data types have very large floating data, so are also called real number. It requires 64 bit i.e. 8-byte storage space.

For Example Program of float and double:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    float number 1 = 10.2;
    double number 2 = 10.7;
 
    printf("number 1 = %fn", number 1);
    printf("number 2 = %lf", number 2);
    return 0;
}
 

Output:

number 1 = 10.200000
number 2 = 10.700000

Derived Data Types in C:

Derived data types are derived from the primary data type by adding some additional relationships with the various elements of the primary data types. The derived data type may be used for representing a single value of multiple values. As these data types have different structures depending on the C-coding, these also called structured data types.

It is further categorized as follows:

  1. Arrays
  2. Function
  3. Pointers

Array in C:

An array is a collection of Identical data elements that are stored under a common variable name. Array refers to a named list of finite numbers of similar data elements. Each of the data elements referenced respectively by a set of conjugate numbers usually (0, 1, 2, 3…..n). If the name of an array of 5 elements in arr, then its elements will be referenced as shown below.

arr[0], arr[1], arr[2], arr[10], arr[20] or multidimensional.

For example:

Marks of 100 students
Salary of 500 lectures.

Function in C:

A function is a blueprint or a reusable code that is made once and used multiple times whenever required. A function is a named part of a program that can be from other parts of the program as is often required.

Syntax:

return_type function_name (argument)  //Declaration
return_type function_name (argument) //Definition
int main()
{
    Your code
return 0;
}

For example: Addition of two integer numbers with user input

#include <stdio.h>
#include <stdlib.h>
int addition(int a, int b)
{
     int sum;
     /* Arguments are used here*/
     sum = a+b;
 
     return sum;
}
 
int main()
{
     int var1, var2;
     printf("Enter number 1: ");
     scanf("%d",&var1);
     printf("Enter number 2: ");
     scanf("%d",&var2);
 
     /* Calling the function here, the function return type
      * is integer so we need an integer variable to hold the
      * returned value of this function.
      */
     int show = addition(var1, var2);
     printf ("Addition is: %d", show);
 
     return 0;
}
 

Output:

Enter number 1: 5
Enter number 2: 3
Addition is: 8

Pointers in C:

A pointer is a variable that holds a memory address. This address is usually the location of another variable in memory. If one variable contains to address of another variable, the first variable is said to point to the second.

In other words, it is a special type of variable which is used to store the address of another variable. It is used for the access memory location.

In this diagram 10 is a value of x and the address of value is 039EEEB7, this address can be changed in different computers as different addresses.

Syntax:

datatype *pointer_name;

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int x=10;
   int *ptr;
   ptr=&x;
   printf("Value of x is: %d\n", x);
   printf("Address of x is: %d",ptr);
 
   return 0;
}
 

Output:

Value of x is: 10
Address of x is: 6356728

Here, if you find the value of x then put *ptr or x and find the address of x so put ptr or &x. & is a sign of address.

Here, if you find the value of x, add * ptr or x and if you find the address of x, add ptr or &x. & is a sign of an address.

User-Defined Data Types in C:

The user-defined data type is used for type definition i.e. it allows the users to define a variable or an identifier, which is used for the representation of existing data types. It has two types.

Example.1 enum identifier { v1, v2, v3, ………..vn};

Here, enum is the reserved word and v1, v2, v3, ………..vn is the value which is called enumeration constants.

Example.2 Data type identifier,

Here, the data type may be int, float, etc. identifiers give us the information about the new name given to the data type.

Typedef is used to represent the existing data type i.e. by using the new type can be used in place of the old data types.

Types of user-defined data type:

  1. structure
  2. union
  3. class
  4. enumeration

Structure:

The structure is a group of variables of different data types represented by a single name. It can not have a function. The structure can not secure the data of the object. The structure is written by struct in C language.

Syntax:

struct structureName 
{
    data type variable name;
};

Union in C:

The size of the union is defined as by the data types which consume a maximum number of bytes. The union is a user-defined data type in which all members share the same memory location.

Enumeration in C:

Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name and it is denoted by enum in C.

If you have any problem in solving your questions, then join our Telegram group or search @Learnsimply, we will help you.

Please write comments if you find anything incorrect in the Data Types lecture.

Leave a Comment

en English
X