Thursday 3 July 2014

GE6151 Computer Programming DATA TYPES



GE6151 Computer Programming DATA TYPES

DATA TYPES

The four basic data types are

INTEGER

These are whole numbers, both positive and negative. Unsigned integers (positive values only) are supported. In addition, there are short and long integers.

The keyword used to define integers is, int

An example of an integer value is 32. An example of declaring an integer variable called sum is, int sum;

sum = 20;

FLOATING POINT

These are numbers which contain fractional parts, both positive and negative. The keyword used to define float variables is,

float

An example of a float value is 34.12. An example of declaring a float variable called money is,

float money;

money = 0.12;

DOUBLE

These are exponetional numbers, both positive and negative. The keyword used to define double variables

is,

double

An example of a double value is 3.0E2. An example of declaring a double variable called big is, double big;

big = 312E+7;

CHARACTER

These are single characters. The keyword used to define character variables is, char

An example of a character value is the letter A. An example of declaring a character variable called letter

is,

Char letter;

letter = 'A';

Sample program illustrating each data type

Example:

#include < stdio.h >

main()

{

int sum; float money; char letter; double pi;

sum = 10; /* assign integer value */

money = 2.21; /* assign float value */ letter = 'A'; /* assign character value */ pi = 2.01E6; /* assign a double value */ printf("value of sum = %d\n", sum );

printf("value of money = %f\n", money ); printf("value of letter = %c\n", letter ); printf("value of pi = %e\n", pi );

}

Sample program output value of sum = 10

value of money = 2.210000 value of letter = A

value of pi = 2.010000e+06

INITIALISING DATA VARIABLES AT DECLARATION TIME

In C variables may be initialised with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialised to 10.

int count = 10;

SIMPLE ASSIGNMENT OF VALUES TO VARIABLES

The = operator is used to assign values to data variables. Consider the following statement, which assigns the value 32 an integer variable count, and the letter A to the character variable letter

count = 32;

letter = 'A'

Variable Formatters

%d decimal integer

%c character

%s string or character array

%f float

%e double

No comments:

Post a Comment