Tabs

Sunday, 6 October 2013

Data Types and Variables in C

Now that you know how to take inputs from users, lets understand what Data-Types are, as they determine the kind of input and output out program can handle.

What are Data Types ?

A data type or simply type is a classification, identifying one of various types of data, such as real-valued, integer or character.

It determines the possible values for that type;

The operations that can be done on values of that type;

The meaning of the data;

And the way values of that type can be stored.

The basic data types in c are:

Int

Float

Char

A complete list of data types is given below

Keyword

Variable Type

Range

char

Character (or string)

-128 to 127

int

Integer

-32,768 to 32,767

short
short int

Short integer

-32,768 to 32,767

long

Long integer

-2,147,483,648
to 2,147,483,647

unsigned char

Unsigned character

0 to 255

unsigned int

Unsigned integer

0 to 65,535

unsigned short

Unsigned short integer

0 to 65,535

unsigned long

Unsigned long integer

0 to 4,294,967,295

float

Single-precision
floating point
(accurate to 7 digits)

±3.4 × 10-38 to
±3.4 × 1038

double

Double-precision
floating point
(accurate to 15 digits)

±1.7 × 10-308 to
±1.7 × 10308

What are Variables and how are they related to data types?

Variables are used to store values temporarily in your program. The life span of a variable can be at most equal to the time the program executes.
As the compiler needs to know what type of data is to be stored (different data types are stored differently as different operations can be performed based on the data type) we assign a data type to each variable.
The syntax to create variables is variable name followed by one or more variables, separated by comma. You can also initialize each variable in the same statement. Also as variables are declared from left to right you can use the value of the variable on the left to assign current variables' value.