My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. No Comments on Data Types in C

A data type specifies the type of value that we use in our programs. A data type is generally specified when declaring variables, arrays, functions etc. In ANSI C, the data types are divided into three categories. They are:

1) Primitive or Fundamental data types

2) User-defined data types

3) Derived data types

 

Primitive or Fundamental data types

The primitive data types in ANSI C are as shown in the below diagram:

data-types-c

The most fundamental data types that every C compiler supports are: int, char, float and double. Other compilers support the extended versions of these fundamental data types like: short int, long int, long double etc. The signed data types are used for storing both positive and negative values where as the unsigned data types are used to store only positive values. The memory capacity of these data types are based on the hardware. The memory capacity of all the fundamental data types on a 16-bit machine is as shown below:

data-types-sizes-in-c

 

User-Defined data types

ANSI C allows the users to define identifiers as their own data types, based on the already existing primitive or fundamental data types. This concept is known as “type definition” and the data types thus created are known as user-defined data types. We can create user-defined data types in two ways:

1) By using the “typedef” keyword

2) By using the “enum” keyword

typedef  Keyword

The “typedef” keyword can be used to declare an identifier as a user-defined data type. The syntax for using typedef is as shown below:

typedef-ex

In the above example, rollno and average are new user-defined data types. Now we can use rollno and average as data types as shown below:

typedef-usage

enum Keyword

The “enum” is used to declare identifiers as user-defined data types. Such data types are also called as enumerations. The “enum” keyword can be used to declare an identifier as data type which can store a limited set of integer values. The syntax of using “enum” keyword is as shown below:

enum-syntax

Example usage of enum is as shown below:

enum-example

In the above example, “days” is a new user-defined type. All the variables created using the “days” data type can only contain values in the range Mon to Sat as shown in the above example. We can declare variables by using the “days” data type as shown below:

enum-usage

If we display “day1” in the printf() statement, it will display it as zero. Generally, the value of a first element in an enumeration always starts with zero and the next element’s value is previous element’s value plus 1. So, the value of “Tue” will be one and so on.

 

Derived data types

The data types which are created using the already existing primitive or fundamental types are known as derived data types. Like user-defined data types we cannot declare new variables using the derived data types. Examples of derived data types in C are: arrays, functions, structures, unions and pointers.

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Leave a Reply

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

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me