0% found this document useful (0 votes)
2 views7 pages

Data Types

Uploaded by

karthikganti007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Data Types

Uploaded by

karthikganti007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

FindMind Collection

2015

Data Types
 Data types refers to an extensive system for declaring variables of different 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.
 The value of a variable can be changed any time
 Please note that there is not a boolean data type. C does not have the traditional view
about logical comparison, but thats another story.

Difference between float and double


Generally the size of float(Single precision float data type) is 4 bytes and that of
double(Double precision float data type) is 8 bytes. Floating point variables has a precision
of 6 digits whereas the the precision of double is 14 digits.
FindMind Collection
2015
Note: Precision describes the number of significant decimal places that a floating values
carries.

User Defined Data type:

C supports the features “typedef” that allows users to define the identifier which would
represent an existing data type. This defined data type can then be used to declare variables:

Syntax:

typedef int numbers;


FindMind Collection
2015

numbers num1,num2;

In this example, num1 and num2 are declared as int variables. The main advantage of user
defined data type is that it increases the program’s readability.

Another type is enumerated type. This is also a user defined data type

Syntax:

enum identifier {value1,value2, value 3,…}

“Enum” is the keyword and “identifier” is the user defined data type that is used to declare the
variables. It can have any value enclosed within the curly braces. For example:

enum day {January, February,March,April,..};

enum day month_st,month_end;

The compiler automatically assigns integer digits beginning from 0 to all the enumeration
constants. For example, “January ” will have value 0 assigned, “February” value 2 assigned
and so on. You can also explicitly assign the enumeration constants.

Few examples for user defined data type:

 Structures
 Arrays
 Union
FindMind Collection
2015

Size & Range:


Size and range of all datatypes are given in the below table.

Type Storage size Value range


char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Type Modifiers:

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
FindMind Collection
2015

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'.

Void Type:
The void type specifies that no value is available. It is used in three kinds of situations :

S.N. Types and Description


Function returns as void
There are various functions in C which do not return value or you can say they
1
return void. A function with no return value has the return type as void. For
example void exit (int status);
Function arguments as void
2 There are various functions in C which do not accept any parameter. A function
with no parameter can accept as a void. For example, int rand(void);
Pointers to void
A pointer of type void * represents the address of an object, but not its type.
3
For example a memory allocation function void *malloc( size_t size ); returns
a pointer to void which can be casted to any data type.

Floating Point Representation:


FindMind Collection
2015

In the above example,


 (-) i.e negative sign will be stored in sign bit.
 248 will be stored in the exponent area
 The number after decimal point will be in mantissa part.

This is the method to store a float number in bits.

Type Conversion:

In the C programming language, a type conversion is the conversion two different sorts
of data type into a common form, in order for them to be manipulated.

In the C language there are different basic data types, such as int, char, float, double; there are also
some user defined data types such as structures, arrays, etc. If the operatoris taking operands of
different data types, then they are converted to a common data types by certain rules. Generally,
automatic conversions are those which can convert anarrower operand into a wider one without loss
of information. For example, converting an integer to floating point in examples
like float + integer (on 64-bit machine). A char is simply a small integer, so chars may be freely used
in arithmetic expressions.

Conversion of char to integer, every character in c corresponds to a particular ASCII value. Anytime
two character are values are operated using arithmetic operator there ASCII values(specific
numerical value) are picked and evaluated. But when a char is converted to int can it ever produce
negative value? The answer varies from machine to machine, reflecting difference in architecture.
On some machines a char whose leftmost bit is 1 will be converted to a negative integer("sign
extension"). On others, a char is promoted to int by adding zeroes at the left most end, and thus
FindMind Collection
2015

always positive. The definition of C guarantees that any character in the machine's standard printing
character set will never be negative, so these characters will always be positive quantities in
expressions.

Relational operators like i>j and logical expressions connected by && and || are defined to have
values 1 if true, and 0 if false. Thus the assignment

d= c>= '0' && c<=='9' sets d to i if c is a digit, and 0 if not. However, function isdigit() may return any
non-zero value for true. In test part of if, while, for, etc. "true" just means any non-zero value, so this
makes no difference. Implicit arithmetic conversions work much as expected. In general, if an
operator like + or * that take two operands (a binary operator) has operands of different types, than
the "lower" is promoted to "higher" type before the operator proceeds. The result is of the type high.

You might also like