Skip to main content

Data Types in C-Programming Language

A program usually contains differnt types of data elements and need to store the values being used in program.
C-language has different data types for different types of data and can be broadly classified as :
1• Primary Data types
2• Secondary Data types

Primary Data Types

The primary data type is used for representing a single value only .It is also called simple or fundamental data type. As these are used at primary level in the high level language ,so these are called primary data type .Simple data types has some standard data types.

    These are subdivided into 4 categories:

1• Integer data type
2• Floating point data type
3• Character data type
4• Void data type

1• Integer Data Types

Generally and integer occupies 2Bytes memory space and its value range  Limited to - 32768 to + 32768 .A signed integer use 1 bit for storing sign and rest 15 bits for number.
Range of an unsigned integer is from 0 to 65535 .The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.

  The general syntax is:
   int <variable name>;

Like :  int num1;
Example: 5,6,100

2• Floating Point Types

These are further divided into 2 types:
(a) Float Data Type:
          Storage size         4 bytes
          Range                    3.4E-38 to 3.4E +38
          Precision              6 digits
  General Syntax is: float<variable name>;
        Like : float num1;
Example: 9.1 , 3.12

(b) Double Data Type:
    Storage size           8 bytes
    Range                      1.7E-308 to 1.7E+308
  General Syntax is: double<variable name>;
        Like : double num;
  Example: 9.125 , 3.1254

3• Character Data Types
    Storage size     1 byte for   both 
                                 signed and unsigned      
                                  chars.
     Range              0 to 255 for unsigned
                                chars
                              -128 to +127 for signed
                                chars

General Syntax is: char<variable name>;
Like : char ch ='a';
Example: a,h,l

4• Void Data 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.

Secondary Data Types

     Secondary data types are derived from the primary data type by adding some additional relationship with the various elements of the primary data types .As these data types have different structure depending on the C-coding these are also called structured data type these are also called Derived data type.

         These are further subdivided into four categories:
1. Array & String Data Types
2. Pointer Data Type
3. Structure & Union Data Type
4. User Defined Data Types

1• Array & String Data Types

  (a). Array Data Type: Array is  set of homogeneous data elements. It is a collection of same types of elements.
       An array is a group of related data items , which share common name.
   General Syntax is:
     data type   array name [size];
     Or
   data type array name[subscript1][subscript2];

Example:
int salary[10]; (integer is of 2 bytes.Size 10 integers means 2*10=20 bytes)

int matrix[2][2];

(b) String Data Type: Combination of characters is called string. String data type is used to store only character data.
   If the string is stored in variable then such type of variable should be of character type.

General Syntax is:
data type string_name[size];
Example:char city[10];
                 char city[10][20];

2• Pointer Data Type
    A pointer is a memory variable that stores memory address. Pointer can have any name that is legal for other variable and it is declared in the same fashion like other variables but it is always denoted by '*' operator.
General Syntax is:
     datatype      *pointer_variable;
Example: int *x;
                  float *f;

3• Structure & Union Data Type
   Structure and union data types are also called heterogeneous data types .They are used to store different types of data elements together using common name.

(a) Structure Data Type: A structured data type is a collection of data elements that are referenced under one name, providing the convenient means of keeping related information together.
General Syntax is:
struct      structure _tag
{
data-type-1       member-1;
data-type-2       member-2;
.
.
.
data-type-n       member-n;
  };

Example:
struct   student
{
char      grade;
int          roll_no;
float      fees;
double  marks;
}st;

Total size=char+int+float+double=
1+2+4+8=15 bytes

(b) Union Data Type: Union is a data type that declares a set of multiple data elements sharing the same name and memory area.
The compiler allocate sufficient memory to hold the largest variable of the union. All the members of union occupy the same memory locations called addresses.
General Syntax is:
union    union _tag
{
data-type-1       member-1;
data-type-2       member-2;
.
.
.
data-type-n       member-n;
  };

Example:
union   student
{
char      grade;
int          roll_no;
float      fees;
double  marks;
}st;

Total size=8 bytes

4• User Defined Type Declaration
  C language supports a feature where user can define an identifier that characterizes an existing data type .This user defined data type identifier can later be used to declare variables .Two types of user defined data types are:

(a) Typedef Data Type: User can define an identifier that represent an existing data type .The user defined data type identifier can later be used to declare variables.
General Syntax is:
typedef <type> <identifier>;
Like: typedef int number;

For example:
"int n1" or "number n1" both statements declaring an integer variable. We have just changed the default keyword "int" to "number" to declare integer variable.

(b) Enumerated Data Type: This is also used for type definition ,that is , it allows the users to define a variable or an identifier which is used for representation of existing data type . In other words ,it provides us a way to define our own data type .

General Syntax is:
enum identifier {v1,v2,v3.....,vn};
Or
enum identifier {v1,v2,v3....,vn}variable;

Here enum is the reserve  word and v1, v 2, v3 ...., vn all are the values which is called enumeration constants.

Example:
enum  month {jan,feb,march};
Or
enum  year {1999,2000,2001}y;

prev
next

Comments

Popular posts from this blog

Top 10 Best Video Editing Apps for Android

            Top 10 Best Video Editing Apps for Android                                                                                                                       Magisto Video Editor & Maker Magisto Video Editor  automatically  turns your photos and video clips into  magical  music videos that you’ll want to share with friends and family on Facebook, Instagram and more. Magisto Video Maker has over  80 million  happy users worldwide and was featured in  Google Play Editors’ Choice , Google's list of  Best Android Apps of 2015  and CES best app of the year! Express your creativity - make a movie an...

Removing HTML Tags from string in C#

Hello friends, With this very first post of our new Blog GimiBits, we start our whole new journey contributing to information technology. Our First post is about the logic to remove the HTML tags from the given string with C# (C Sharp) programming language. Logic is very Simple to Implement, we take up a string and for its each index char we find the opening '<' braces and ignores the inside content until the closing '>' braces is not encountered and save to another char[] datatype. Here is a function based Implementation of the logic:  public string RemoveHtmlTags(string Html)         {             string content = Html;             char[] array = new char[content.Length];             int arrayIndex = 0;           ...

New Website Ideas to start a great Business online and earn money.

Internet is an all time business today. We know there is plenty of people with entrepreneurial passion waiting for the idea to finally start something meaningful. We also know there is a lot of people not satisfied with product options they have at the moment. So why not to try bring these two together? today in this post i came up with some the new unique ideas that can be implemented Today for a great start up. Here is list : 1. Help choosing career / work / occupation I think most of people have real problem while choosing career or life path. The site would help to make decision. In a nutshell - idea is that there will be two types of users - ones who seeks for advice and those who will help to make decision. In order to get career advice you will have to choose three activities which you love to do. Based on your chosen activities others will offer what possible career path you could choose. 2. Startup failures You can learn a lot from failures of others. W...