Data Types: Declaring and Initializing Variables, Type Conversion
Data Types: Declaring and Initializing Variables, Type Conversion
Data Types
C supports several types of data, each of which may be represented differently within the computers memory.
The basic data types can be augmented by the use of the data type qualifiers short, long, signed and unsigned.
Designed by Parul Khurana, LIECA.
long
float double long double
32 bits
32 bits 64 bits 80 bits
-2,147,483,648 to 2,147,483,647
3.4 * ( 10**-38) to 3.4 * ( 10**+38) 1.7 * ( 10**-308) to 1.7 * ( 10**+308) 3.4 * ( 10**-4932) to 1.1 * ( 10**+4932)
Declaring Variables
A declaration associates a group of variables with a specified data type. All variables must be declared before they can appear in executable statements. A declaration consists of a data type, followed by one or more variable names, ending with a semicolon.
Designed by Parul Khurana, LIECA.
Thus , a, b and c are declared to be integer variables, root1 and root2 are floating-point variables and flag is a char-type variable.
Designed by Parul Khurana, LIECA.
float root2;
This form may be useful if each variable is to be accompanied by a comment explaining its purpose. In small programs, however, items of same type are usually combined in a single declaration.
Designed by Parul Khurana, LIECA.
Initializing Variables
Initial values can be assigned to variables within a type declaration. To do so, the declaration must consist of a data type, followed by a variable name, an equal sign(=) and a constant of the appropriate type. A semicolon must appear at the end, as usual.
Designed by Parul Khurana, LIECA.
Thus, a, b and c are integer variables whose initial values are 12, 2400 and -320, root1 and root2 are floating-point variables whose initial values are 3.12 and -8.2 and flag is a char type variable initially assigned the character *.
Designed by Parul Khurana, LIECA.
Type Conversion
When variables of one type are mixed with variables of another type, a type-conversion will occur. In an assignment, the type conversion rule is easy:
The value of the right side(expression side) of the assignment is converted to the type of the left side (target variable), as illustrated here:
f=x
// line 4
In line 1, the left high order bits of the integer variable x are lopped off, leaving ch with the lower 8 bits. If x were between 255 and 0, ch and x would have identical values. Otherwise, the value of ch would reflect only the lower-order of bits of x.
Designed by Parul Khurana, LIECA.
f=x
// line 4
In line 2, x will receive the non-fractional part of f. In line 3, f will convert the 8-bit integer value stored in ch to the same value in the floatingpoint format.
Designed by Parul Khurana, LIECA.
f=x
// line 4
In line 4, except that f will convert an integer value into floating-point format.
In many 16-bit environments, this means that 8 bits will be lost when going from an integer to a character and 16-bits will be lost when going from a long integer to an integer.
Designed by Parul Khurana, LIECA.
These kind of conversions only change the form in which value is represented.
Designed by Parul Khurana, LIECA.
long int
long int float double long double
High-order 16 bits
None Fractional part and possibly more Precision, result rounded Precision, result rounded
Practice Questions
Give suitable declarations for the variables to hold the values for : Your name. Your age ( can never be negative ). Your %age marks.