Datatype and Typeconversion
Datatype and Typeconversion
2015
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.
Please note that there is not a boolean data type. C does not have the traditional view
about logical comparison, but thats another story.
Note: Precision describes the number of significant decimal places that a floating values carries.
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:
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.
Structures
Arrays
Union
Size and range of all datatypes are given in the below table.
Type 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:
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:
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.