BASIC DATATYPES AND SIZES


Data Type in a programming language is a set of data with values having predefined characteristics.
The data type defines:
  1. The amount of storage allocated to variables.
  2. The values that they can accept.
  3. The operations that can be performed on variables.

Data types are classified into Three.
  1. Primary data type
  2. Derived data type
  3. User-defined data type
  4. void or Empty data type

1. Primary data type
All C Compilers accept the following fundamental or Primary data types.
(a)   integer
(b)  floating point
(c)   character
(d)  double
(

(a) integer:- Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely “short int”, “int” and “long int”. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. The long and unsigned integers are used to declare a longer range of values.

(b) floating point:- Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float.

(c) character:- A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.

(d) double:- When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. A double data type number uses 64 bits with a precision of 14 digits. To extend the precision further we can use long double which consumes 80 bits of memory space.






2. Derived data type
Derived data type is a data type which is derived from intrinsic data types or previously defined primary types. The set of values for a specific derived type consists of all possible sequences of component values permitted by the definition of that derived type. Examples for Derived data types are arrays, functions, structures, unions and  pointers etc.

3. User-defined data type

In C language a user can define an identifier that represents an existing data type. The user defined data type identifier can later be used to declare variables. typedef and enum are examples for user-defined data types.


(a) typedef:- typedef is a user-defined data type used to define an identifier that would represent an existing data type.

Syntax:- typedef  type  identifier;

Here type represents existing data type and ‘identifier’ refers to the new name given to the data type.

Eg:-     #include<stdio.h>
#include<conio.h>
void main()
{
            typedef  int  number;
            number a, b, c ;
            printf(“Enter teo numbers: “);
            scanf(“%d%d”,&a,&b);
            c=a+b;
            printf(“Sum=%d”,c);
}
           
(b) enum:- An enumerated data type is a list of possible values, each of which is assigned a sequential number. This allows us to write code that can compare values easily.

Syntax:- enum identifier {value1, value2 …. Value n};

The identifier is a user defined enumerated data type which can be used to declare variables that have one of the values enclosed within the braces. After the definition we can declare variables to be of this ‘new’ type as below.

enum identifier V1, V2, V3, ……… Vn

The enumerated variables V1, V2 .. Vn can have only one of the values value1, value2 .. value n.
Eg:-

enum day {Monday, Tuesday, …. Sunday};
enum day week_st, week end;
week_st = Monday;
week_end = Friday;
if(wk_st == Tuesday)
      week_en = Saturday;

4.  The void type has no value. Using void data type, we can specify the type of a function. The type of function is said to be void when it does not return any values to the calling function.

No comments:

Post a Comment