Data Types in C (types and examples)


Hello Guys, Let's see about Data Types with example 








Data Types:-

There are various types of data. For example, data 10 and 100.5 are data of different types. The data 10 is an integer number (i.e whole number) while 100.5 is a fractional number. There are other varieties of data types supported by C, each of which may be represented differently within computer's memory. The variety of data types available allow the programmer to select the type needed by the application.

(ANSI) C supports three  classes of data types:-

1). Primary (fundamental) data types
2). User-defined data types
3). Derived data types 



1). Primary Data Types 

The primary data types and derived data types are discussed in this section. The user-defined data types such as arrays, functions, structures and pointers are discussed in separate chapters. 

Primary data types are categorized into five types:-

a) Integer type (int)
b) Floating point type (float)
c) Double-precision floating point type (double)
d) Character type (char)
e) void type (void

The words in parentheses indicate the keyword used in C to indicate the data type. 


a) Integer Types


Integers are whole numbers, (i.e. non-fractional numbers. Generally integers require 16 bit of storage. C has further three classes of integer-integer(int), short integer(short integer) and long integer(long int), in both signed and unsigned forms. The appropriate data types is used according to our requirement, i.e. what type of number is to be used.
                 


b) Floating Point Types

Floating point types are fractional numbers (i.e. real numbers). They are defined in C by the keyword float. Floating numbers reserve 32 bits (i.e. 4  bytes) of storage, with 6 digits of precision. 
                   

c) Double-Precision Floating Point Types


When the accuracy provided by a float number is not sufficient, the type double can be used to define the number. A double data type number used 64 bits (8 bytes) giving a precision of 14 digits. These are known as double precision numbers. To extend the precision further, long double can be used which uses 80 bits (10 bytes) giving 18 digits of precision.
                   

d) Character Types


A single character can be defined as a character type data. Characters are stored in 8 bits  (1 byte). The qualifier is char. The qualifier signed or unsigned may be used with char. The unsigned char has values between 0 and 255. The The signed char has values from -128 to 127. The conversion character for this type is C. 


e) Void Type

The void type has no values. This is usually used to specify a type of function when it does not return any value to the calling function. This discussed in more detail in function chapter.   





2). User-Defined Data Types


C supports a feature called type definition which allows users to define an identifier that would represent an existing data type. The typedef statement is used to give new name to an existing data type. It allows users to define new data types that are equivalent to existing data types. it takes the general form.

typedef 

Here, existing_data_type refers to  any one of the fundamental or user-defined data types;new_name_for_existing_data_type refers to a new identifiers name. Some of
typedef are:- 

i) typedef int integer;
Here, integer symbolizes int data type, Now we can declare int variable a as integer a instead of int a (but int a is still valid)
i.e. int a is equivalent to integer a.

ii) typedef float decimal;
Here, float f is equivalent to decimal f
Thus, typedef statement is used to alias existing data types as convenient.   





3). Derived Data Types

There are some data types which are derived from the existing primary data types. 

For example:

         struct  st{
         int a;
         float b;
         char c;
         }

Here, 'struct' is a derived data types 'structure'. It consists of integer. Float and character variable. Structures, unions and enumerations are the derived data types used in C.   



Thanks for all......                                              

Comments

Post a Comment

Popular posts from this blog

Basic Structure of C Program. (Explain with Example)

Learn C Programming Languages ( Definitions and Advantages).