i2tutorials

Data Types

Data Types

 

The C data types refer to an extended system used to declare variables or functions of different types. The type of a variable determines the amount of space in the storage and interpretation of the stored bit pattern. It also specifies the valid operation on the type.

 

The types in C are classified as follows:

  1. Primary or fundamental data type
  2. Derived data type
  3. User-defined data type
  4. Void data type

 

Primary or Fundamental Data Type:

 

The various primary data types supported by the C language are:

  1. Integers
  2. Real
  3. Character
  4. Void

 

Integer Types:

 

The keyword for integers is int. The integer data type requires 2 bytes of storage in windows and 4 bytes of storage in a Linux environment. Integer data types are of two types, namely signed (both positive and negative numbers) and unsigned (only positive numbers).

 

Floating-Point Types:

 

The keyword for real numbers is float. The storage requirement for the floating data type is 4 bytes.

 

Character Data Types:

 

The keyword for character data type is char. The memory required for char data type is 1 byte. If a data type needs n-bits of storage then-

 

Data Types

If a data type needs n-bits of storage then:

 

 

Format Specifiers C:

 

It is associated with data types because it defines the type of data to enter or print when using I/O declarations.

 

Derived Data Type:

 

The data types that have been derived from fundamental data types are called derived data types.

Examples: Arrays, Functions, Pointers.

 

Arrays:

 

Collection of data elements of similar data types, which is accessible by common name.

 

Pointers:

 

Pointers are the main variables that are used to store the storage address of another variable.

 

Functions:

 

This is a group of statements written to accomplish a particular task. The functions are either user-defined or integrated with the library.

 

User-Defined Data Type:

 

Structures:

 

This is a user-defined data type in which a collection of different datatypes can be made and accessible across an object.

 

Union:

 

A special type of data type which enables us to store various types of data in the same memory location.

C supports the feature typedef that allows users to define the identifier which would represent an existing data type. This defined data type may then be used to declare variables.

 

Syntax:

 

typedef int numbers; 
numbers num1, num2;

 

Here, num1 and num2 are declared as ‘int’ variables. The main advantage of the user-defined data type is that it enhances the readability of the program.

Another type of user-defined data type is the enumerated type.

It refers to the arithmetical data types used to define variables to specify only some discrete integer values throughout the program.

 

Syntax:

 

enum identifier {value1, value2, value 3,…};

 

“enum” is the keyword and “identifier” is the user-defined data type that is used to declare the variables. It can be any value included in curled braces.

 

Example:

 

enum day {January, February,March,April,..}; 
enum day month_st,month_end;

 

The compiler automatically assigns integer digits beginning at 0 to all enumeration constants. For example, “January” will have value 0 assigned, “February” value 2 assigned and, so on. You can also explicitly assign the enumeration constants.

 

Void Data Type:

 

The null type indicates an empty set of values. It is used as a return type for functions that generate no value. The void type never refers to an object and hence is not included in any reference to object types. The void type has no values and only one operation – assignment.

 

 

 

Exit mobile version