DATA TYPES

|


C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.
“Data types” are defined as ways or means to identify the type of data and associated operations of handling it.


Generally c have following three types of data types
(1) Fundamental or Built-in or Primitive Data Types

(2) User Defined Data Types

(3) Derived Data Types




(1) Fundamental or Built-in or Primitive Data Types

C has the following basic built-in data types.
  •   int
  •   char
  •   float
  •   double
  •   void
  • Integer Types
Integers are whole numbers with a range of values, range of values are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32768 (that is, -215 to +215-1). A signed integer use one bit for storing sign and rest 15 bits for number.
To control the range of numbers and storage space, C has three classes of integer storage namely short int, int and long int. 

All three data types have signed and unsigned forms. A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore the range of an unsigned integer will be from 0 to 65535. The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.

Syntax:

int variable_name; /*declaration*/

short int num2;      /*declaration*/

long int num3;       /*declaration*/

Example: 5, 6, 100, 2500.

Integer Data Type Memory Allocation

  • Floating Point Types:
The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float. 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 and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space.

Syntax: float variable_name; /*declaration*/


float num1;                            /*declaration*/

double num2;                        /*declaration*/

long double num3;               /*declaration*/

Example: 9.125, 3.1254.

  •   Character Type:
Character type variable can hold a single character. As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.

Syntax:
char ch; /*declaration*/


char ch = ‘a’; /*declaration + initilasization*/

Example: a, b, g, S, j.



  • Void Type:
The void type has no values therefore we cannot declare it as variable as we did in case of integer and float.
The void data type is usually used with function to specify its type. Like in our first C program we declared “main()” as void type because it does not return any value. The concept of returning values will be discussed in detail in the C function hub.

Modifier
The data types explained above have the following modifiers.

  •   short
  •   long
  •   signed
  •   unsigned

The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long int

float <= double <= long double

What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is:
You can find out how much storage is allocated to a data type by using the sizeof operator discussed in Operator Types Session.
Here is an example to check size of memory taken by various data types.

For more details on modifier click here.



Program to find out the size of data types :

main()
{
   printf("sizeof(char) == %d\n", sizeof(char));
   printf("sizeof(short) == %d\n", sizeof(short));
   printf("sizeof(int) == %d\n", sizeof(int));
   printf("sizeof(long) == %d\n", sizeof(long));
   printf("sizeof(float) == %d\n", sizeof(float));
   printf("sizeof(double) == %d\n", sizeof(double));
   printf("sizeof(long double) == %d\n", sizeof(long double));
   printf("sizeof(long long) == %d\n", sizeof(long long));
   return 0;
 }

Qualifier
A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:
The value of a variable can be changed.
The value of a variable must always be read from memory rather than from a register
Standard C language recognizes the following two qualifiers:

  •   const
  •   volatile
Const :
The const qualifier is used to tell C that the variable value can not change after initialisation.
const float pi=3.14159;
Now pi cannot be changed at a later time within the program.
Another way to define constants is with the #define preprocessor which has the advantage that it does not use any storage


Volatile :
The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler (such as a variable updated by the system clock or by another program). This prevents the compiler from optimizing code referring to the object by storing the object's value in a register and re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in "C". So for now just proceed.



Size of data type in c (Turbo C++ compiler)

Only two modifiers short and long can affect the size of any data type in c. size of primitive data type in c depends upon the word length of the microprocessor. In general we can say size of int is word length of microprocessor.






Size of data types in the 16 bit compilers, like TURBO c++, Borland c++ etc.


Size of data type in c (Linux gcc compiler)

Size of data types in the 32 bit compilers. Example: LINUX gcc compiler.



(2) User Defined Data Types
C support user-defined data types. Once a user-defined type has been established, then new variables, array, structures etc. can be declared in the terms of this new data type. In C language user-defined data types are: typedef & enum.

  •   typedef
  •   enum
  •   structure
  •   union
  • Typedef
typedef is an abbreviation used for “Type Definition”. Its purpose is to redefine the name of an existing data type. This can be later used to declare variables. Its general form is:
typedef standard-datatype userdefined-datatype.
Ex:
(i) typedef int age;
int x;
age p,q,r,s;
Here, all the variables are holding integer value but age helps us to understand
that these 4 variables will hold age. It helps users debugging the program.
(ii) struct student

{
char name [30];
int roll_no;
float percent;
};
struct student s;
Using typedef:
struct student

{
char name [30];
int roll_no;
float percent;
};
typedef struct student STU;
STU s1, s2;

  • Enum
The enumerated data type gives us an opportunity to invent our own data type and define what values the variables of this data type can take. Its general form is:
enum datatype-name {val1,val2,…..,valn};
Ex:-
(i)
enum weekdays
{Sunday, Monday, Thursday, Wednesday, Thursday, Friday, Saturday};
weekdays x, y;



(ii)
enum marks {gradeA=1, gradeB=2, gradeC=3};
enum marks s1, s2;
The values declared by enum are ordinal nos. Ordinal values starts from zero.

  •  Structures
A Structure can be defined as a collection or a group of variables that are referenced under one name. it is used to keep related information together. Here we use a ‘struct’ keyword to construct a structure.
Example:
Struct bank
{
Char name[30];

Int account_no;

Int amount;
};
Struct b;


In the above example bank is the name of the structure, then we have declared the variables which are the elements of this structure. And b is the object of the structure which is used to refer the elements of the structure.

Elements will be referred as :
b.amount=5000;
b.account_no=2546321;

  • Union
Groups variables which share the same storage space.
A union is similar to a struct, except it allows you to define variables that share storage space. The syntax for defining unions is:

union [union-type-name] { type variable-names; ... } [union-variables] ;


For example,union short_or_long { short i; long l; } a_number;


The compiler will allocate enough storage in a number to accommodate the largest element in the union. Elements of a union are accessed in the same manner as a struct.




Unlike a struct, the variables 'a_number.i' and 'a_number.l' occupy the same location in memory. Thus, writing into one will overwrite the other.






(3) Derived Data Types

Derived data types in C Programming Language are those C data types which are derived from the fundamental data types using some declaration operators.
  • Array
  • Function
  • Pointers
Arrays:
Arrays can be defined as a set of finite and homogeneous data elements. Each element of an array is referenced using an index.
For example:
If we the name of an array is AR which have 5 elements then the array will be represented as :
AR[0], AR[1], AR[2], AR[3], AR[4]
Here, these subscripts which are containing the digit is known as an index.
There are various types of arrays namely:


* One dimensional

* Two dimensional

* Multi dimensional

The elements of an array are indexed from 0 to size-1. It is necessary to declare the size of an array before initialization. An array can be initialize by placing the elements of an array within the curly braces.
For example, an array initialization can be done in following manner:

int AR[5] = {5, 2, 6, 8, 3};
A sample program of an array:

Void main()

{

int arr[10];

arr[10] = {1,5,4,6,3,9,14,81,7,11};

printf(“\n %d”,arr[i]);

}

2. Functions
A function can be defined as a part of the program which is declared by the programmer in any part of the program and a function can be of any name depending upon the choice of the programmer. The function declared can be invoked from other part of the program.
Example of a function:
#include

Float square(float);

Void main()

{

Float num=3.14;

Float sq;

Sq=square(num);

Printf(“%f”,sq);

}


Float square(float x)

{

Return x * x;

}

The above code will print the square of the above given number. square is the name of the function used. And sq is the variable used to store the value of the square. The use of the function before the main function is known as declaration of the function, then comes the definition of a function which takes place outside the main function. Then finally comes the calling of the function which takes place inside the main function.

3. Pointers
A pointer is a variable that holds the address of the memory space. We can say that if one variable can hold the address of the another variable then it is said that the first variable is pointing to the second.
A pointer is declared in a following manner:


int *temp;


i.e. first we have to declare the type and then the variable name precede by an * (asterisk) sign.

The pointer variable always occupies 2 bytes of memory.

1 Comentário:

Anonymous said...

Thank you for your information. It very nice article.
Java Training in Pune

Post a Comment