Data Types in C

Nov 26 • Notes • 1323 Views • No Comments on Data Types in C

Compiler is a software designed to execute any program. But it needs be told before – hand what type of data will be fed. Here comes the concept of Data Types.

So data types of data that is to be used in the program. In C there are two categories of data types:-

1.     Pre – defined data types

2.     User – defined data types

Data Types in CPre – defined data types are those data types which are defined previously and stored in the libraries of C. They needs to be just called for the use.

But the user – defined data types are those which are not defined previously. The user defines it according to the need with the help of some predefined reserved keywords. So these are more flexible and very useful.

Pre – defined data-types are such as int, float, char, double, void.

The user – defined data – types can be defined using the following reserved keywords. Those keywords are typedef, enum, structure, union.

Pre – defined data types:-

Syntax:-

<data type> <variable name>;

<variable name> = <value>;

where <data types> are those which are defined below.

           <variable name> are the name assigned by you to store that type of value.

           <value> are the data which you want to store.

  • Integer:- This type of data is generally referred to the numeric values or whole numbers. The keyword used to define this type of variable to store data is “int”.
  • Float:- This type of data is generally referred to the fractional or decimal number values. The keyword used to define this type of variable to store data is “float”.
  • Character:- This type of data is generally referred to the alphabetic values. These types of variables can store one alphabet at a time. The keyword used to define this type of variable to store data is “char”.
  • Double:- This type of data is generally is also used to refer the fractional or decimal values but with the larger precision or more after decimal places. The keyword used to define this type of variable to store data is “double”.

User – defined data types:-

  • Typedef:– This is used to change the name of a pre – existing data – type. The keyword used is “typedef”.

Syntax:-

typedef <type> <new_type>;

where “typedef” is the reserve word.

           “<type>” is the existing data-type.

           “<new_type>” is the new type.

Example:

typedef float abc;

abc x;          // this declaration is same as “float abc;”

  • Enumeration:- This is used to create a new data-type. The content of the datatype can be given at declaration time. The keyword “enum” is used.

Example:

enum color

{

Blue,

Black,

yellow

};

  • Structure:- This is used to group a certain number of data-types. It is used to store values. When there are many number of data – types stored in structure the memory it occupies is the sum of all the data-types used. For example, if we use int (2 bytes), float (4 bytes), then the structure occupies 6 bytes (int + float) memory.

Example:

struct abc

{

int a;

float b;

double c;

};

  • Union:- It is almost same as structure but the difference is that in this the memory occupied is equal to the the memory occupied by data-type with greatest memory. For example , if we use int (2 bytes) and float(4 bytes) then memory occupied by union is 4 bytes.

Example:

union abc

{

int a;

int b;

float c;

};

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

« »