0% found this document useful (0 votes)
2 views

Datatype and Typeconversion

The document provides an overview of data types in C programming, including their definitions, size, and range. It explains the differences between float and double, user-defined data types, type modifiers, and the void type. Additionally, it covers type conversion rules and how different data types interact in operations.

Uploaded by

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

Datatype and Typeconversion

The document provides an overview of data types in C programming, including their definitions, size, and range. It explains the differences between float and double, user-defined data types, type modifiers, and the void type. Additionally, it covers type conversion rules and how different data types interact in operations.

Uploaded by

SUGANTHI V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

FindMind Collection

2015

Data Types & Type Conversion


 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


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

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;

numbers num1,num2;
FindMind Collection
2015
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

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

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:

SNO Types and Description

1 Function returns as void


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

Floating Point Representation:

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 operator is 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.
FindMind Collection
2015
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 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