Explain Different Data Types Available in ‘C’?

C language supports a rich set of data types the variety of data types available allow the programmer to select the type appropriate to the needs of the application.

ANSI C supports 3 class of data types

1. Primary (or fundamental) data types
2. Derived data types
3. User  defined data types

datatypes

All c compilers support 5 fundamental data types, namely integer (int), character (char), floating point (float), double – precision floating point (double) & void. Many of them also offer extended data types. such as long int & long double.

datatypes1

Data types are representation of data which is stored in the memory. Each variable in a program should be associated with a single Data type.

Data type tells the compiler that :

1. What type of data the variable can hold &
2. How much memory is allocated for that variable.

Data type
Rage of values
Char
– 128 to 127
Int
– 32,768 to 32767
Float
3.4e.38 to 3.4e + 38
Double
1.7 e – 308 to 1.7e + 308

Integer types:

Integers are whole no_s with a rage of values supported by a particular machine. It’s size vary (16 or 32 bits) from machine to machine.

It’s size is 16 bits then –25 to 215. It use one bit  for sing & remaining 5 bits for magnitude of the no_ in the same way 32 bits size is -231 to 231.

C has 3 classes of integer storage, namely short int, int & long int, in both signed & unsigned Forms.

Int:It occupies 2 bytes of memory. The keyword int is used for this

Signed
Unsigned
Range – 32,768 to 32,767
0 to 65, 535
Format specifier % d
% u
Short int: it occupies 1 byte of memory
Signed
Unsigned
Range – 128 to 127
0 to 255
Format code % hd


Long int: If occupies 4 bytes of memory. The keyword long int is used for this % d format code is used for this.

Float:

Floating point (real) no-s are stored in 32 bits with 6 digits of precision i.e it occupies 4 bytes in memory.These are defined by the keyword float.Format specifier for float is % f.

Double:

A double data type number uses 64 bits giving a precision of 14 digits. i.e it occupies 8 bytes in memory. These are known as double precision numbers.

To extend the precision further, we may use long double which uses 80 bits. i.e it occupies 10 bytes of memory

Double
Long double
Keyword double
Longdouble
Format specifier % f (or) % l
% lf (or) % L
Range            1.72.308 to 1.7e + 308
3.4e – 4932 to 1.1e + 4932

Void:

The void type has no values. This is usually used to specify the type of functions. the type of a function is said to be void when it does not return any value to the calling function.

Character type:

A single character can be defined or a character data type. Characters are usually stored in 8 bit (i.e 1 byte) of internal storage.

The qualifier signed or unsigned may be explicitly applied to character. While unsigned character have values between q to 255,signed character have values from–128 to 127.

Derived data Types:

Arrays, functions,structures, unions, pointers.

User defined Data Types:

Typedef :

C supports a feature known as “ type definition” that allows users to define as identifier that would represent an existing data type. The user defined data type identifier can later be used to declare variable.

Syntax:typedef type identifier;

Where type refers to an existing data type & identifier refers to the new name given to the data type.

The existing data type may belong to any clans of type. typedef cannot create a new type.

Ex: typedef int units;
      typedef float marks;

Here units symbolizes int & marks symbolizes float. They can be used to declare variable as

      units batch 1, batch 2,
      marks name 1 [50], name 2 [50]

batch1 & batch 2 are declared as int variables & names [50] & name 2 [50] are declared as 50 cells.floating point array variables.

The main ad of typed is that we can create meaningful data type names for increasing the readability of the program

Enum:

Another user-defined data type is enumerated data type provided by ANSI standard.

Syntax: enum identifier {val 1, val 2, …...val n};

The identifier is a user-defined enumerated data type which can be used to declared variables that can have one of the values enclosed within the braces.

The enumerated data type gives you an opportunity that the user define his own data type consisting a set of named constants called enumerators. 
.
We can declare variables to enum as

Syntax: enum identifier v1, v2…..vn;

The enumerated variables v1, v2, ……,vn can only have one of the values vol1, vol 2, …..valn.

V1 = val2;
V5 = val1;

The compiler automatically assigns integer digits beginning with 0 to all the enumeration constants. i.e val1 is assigned 0, val2 is assigned 1 and so on

You can also assign your own integer constants to them as shown bellow.

ex:enum day { mon=5, tue , ……,sat};

Here the constant mon is assigned the value 5. The remaining constants are assigned values that increase successively by 1.

Ex:  enum day { mon, tue , ……sat};
        enum day week-st, week-end;
        week-st = mon;
        week- end = fri;
         if (week-st = = tue)
         week-end = sat;

Related

C Language 1956346978929044378

Post a Comment

emo-but-icon

item