Sunday, 6 October 2013

Data Types and Variables in C

Data Types 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


KeywordVariable TypeRange
charCharacter (or string)-128 to 127
intInteger-32,768 to 32,767
short
short int
Short integer-32,768 to 32,767
longLong integer-2,147,483,648
to 2,147,483,647
unsigned charUnsigned character0 to 255
unsigned intUnsigned integer0 to 65,535
unsigned shortUnsigned short integer0 to 65,535
unsigned longUnsigned long integer0 to 4,294,967,295
floatSingle-precision
floating point
(accurate to 7 digits)
±3.4 × 10-38 to
±3.4 × 1038
doubleDouble-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.

Example:  int a = 9, b = 2*a;
                  this way a=9 and b=18

Lets understand each data type in detail.
  • Understanding integer data type.
  • Understanding float data type.
  • Understanding character data type.

No comments:

Post a Comment