Monday, 25 October 2010

Data Type

Data Type:

It is a Type of Data.
Which specifies what kind of data and that can contain a Specific Type or Range of Values.
When computer programs store data in variables, each variable must be assigned a specific data type.

In C, variable(data) should be declared before it can be used in program. Data types are the keywords, which are used for assigning a type to a variable.

Whenever we declare variable in Computer’s memory, Computer must know the type of the data to be stored inside the memory.
If we need to store the single character then the size of memory occupied will be different than storing the single integer number.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C.


Data types in C

1. Basic Data types (Fundamental Data Types)
2. Derived Data types
3. User defined Data types


1. Fundamental Data Types

Data Type                 keyword        Description
Integer Data Type         int                Stores the Integer Value
Float Data Type         float                Stores the Floating Point Value
Character Data Type char                Stores the Single Character Value
Long Data Type         long                Stores the Long range Integer Value
Double Data Type        double        Stores the long range Floating Value


C Data types        storage Size Range
        char                            1                 –127 to 127
         int                            2               –32,767 to 32,767
        float                            4                  1E–37 to 1E+37 with six digits of precision
        double                    8                  1E–37 to 1E+37 with ten digits of precision
        long double           10                  1E–37 to 1E+37 with ten digits of precision
        long int                   4                 –2,147,483,647 to 2,147,483,647
        short int                   2                –32,767 to 32,767
        unsigned short int   2                  0 to 65,535
signed short int           2                 –32,767 to 32,767
long long int                   8                 –(2power(63) –1) to 2(power)63 –1
signed long int                  4                  –2,147,483,647 to 2,147,483,647
unsigned long int           4                  0 to 4,294,967,295
unsigned long long int   8                  2(power)64 –1


Format specifier         
            Characters matched
Argument type

%c      

any single character
            Char
%d, %i           
Integer
            integer type

%u      

Integer
            unsigned
%o      

octal integer
            unsigned
%x, %X          

hex integer
            unsigned
%e, %E, %f, %g, %G

floating point number
            floating type
%p      

address format
            void *
%s      
any sequence of non-whitespace characters
            char




Integer types
Floating Type
Character types

2. User Defined Data types
   • Structures
   • Enum

Syntax for declaration of a variable
data_type variable_name;

Integer data types

Keyword int is used for declaring the variable with integer type. For example:
int var1;


Here, var1 is a variable of type integer.
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1
Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1. If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -2147483648,  program will not run correctly.

I/O of integers in C

Floating types

Variables of floating types can hold real values(numbers) such as: 2.34, -9.382 etc. Keywords either float or double is used for declaring floating type variable. For example:

float var2;
double var3;

Here, both var2 and var3 are floating type variables.
In C, floating values can be represented in exponential form as well. For example:
float var3=22.442e2
I/O of characters and ASCII code
Difference between float and double
Write a c program to demonstrate all basic data types available in c.

No comments:

Post a Comment