Thursday 5 December 2019

C Tutorial - Data Types with Examples

Data Types

Introduction

To communicate with the computer, it is required to pass an appropriate value to the computer from the keyword.

The value passed may be of different types, viz. Single character, integer, decimal, string.

In each, the memory required is of different size. Such a defined type of value is called data type.

C supports several different types of data, each of which may be represented differently within the computers memory.
The memory requirement of each data type will determine the permissible range of values for that data type.

C data type can be broadly divided in two categories:

(a) Primary data types.
(b) Secondary data types.

Primary data types

Primary data types are independent data types. It's not derived from other basic or secondary data types.

There are five primary data types in C language:

1. Void
2. Character
3. Integer
4. Float
5. Double

Secondary data types

Secondary data types are also called as derived data types. Secondary data types are derived from the basic data types.

They are also five in number:

1. Array
2. Pointer
3. Structure
4. Union
5. Enum


Primary Data Type

1. Void

Description:
Used to specify empty set containing no values.
Storage Space: 0 byte.
Format: (void).
Range of values: ______.

2. Character (Denoted as "char" in C programming language)

Description:
A character denotes any alphabet, digit or special symbol used to represent in formation and it is used to store a single character.
Storage space: 1 byte
Format: %c
Range of Values: ASCII Character Set.

3. Integer (Denoted as "int" in C programming language)

Description:
Integer type is used to store positive and negative integer.
Storage space: 2 bytes.
Format: %d
Range of values: -32768 to +32767.

4. Float

Description:
It is used to store real number, with single precision floating point number (precision of 6 digits after decimal points.)
Storage space: 4 bytes.
Format: %f
Range of values: -3.4*1038 to +3.4*1038.

5. Double

Description:
It stores real numbers with double precision. The use of double doesn't guarantee to double the number of significant digits in our result, but it improves the accuracy of the arithmetic and reduces the accumulation of rounding errors.
Storage Space: 8 bytes.
Format: %ld
Range of values: -1.7*10308 to +1.7*10308.


Explanation: Numeric data stored is stored in the memory in their binary form, while the character data has to be codified as a unique integer and that code number is stored in the internal storage.

The integer equivalents of alphabets are:

Lower case: a-z = 97 to 122
Upper case: A-Z = 65 to 90

In the above program when the characters are displayed in the integer format the correspondingASCII code are displayed, similarly when integers are displayed in the character format then equivalent character is displayed.

Secondary Data Type

1. Array:

It is a collection of data of similar data type.
e.g. Int num [5];

Reserve a sequence of 5 location of two bytes each for storing integers.

2. Pointer:

Pointer is a variable that stores the address of some other variable.
e.g. int *i;

The above statement declares i as pointer to integer data type.

3. Structure:

A structure is a collection of data of different data types under one name.
e.g.
Struct employees
{
char Name[10];
int Age;
int Salary;
}

4. Union:

It is a collection of data of different types sharing common memory space.
e.g. Union item

{
int m;
float x;
char c;
} ;

5. Enumerated Data types:

This data types gives us an opportunity to invent your own data type and define what values the variable of this data type can take.
Example: enum colors

{
red, green, blue, cyan
};
colors foreground, background;

Here the declaration has two parts:

a) The first part declare the data type and specifies its possible values.
b) The second part declare variable of this data type.

Now we can give the values to these variables:

foreground=red;
background=blue;

But remember we can't use values that aren't in the original declaration. Thus, the following declaration cause error.

foreground=yellow;

Note: Secondary data type has been given in detail later.


Previous Page                                  Next Page

More Topics











100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...