Sunday, June 30, 2013

Data Types in C

In C we have many data types and each data type has its own range and its own specialization. Here is the table which gives you details about the data types and the range of each data type we are providing.

Variable Type
Keyword
Bytes Required
Range
Character
char
1
-128 to 127
Integer
int
2
-32768 to 32767
Short Integer
short
2
-32768 to 32767
Long Integer
long
4
-2,147,483,648 to 2,147,483,647
Unsigned character
unsigned char
1
0 to 255
Unsigned integer
unsigned int
2
0 to 65535
Unsigned short integer
unsigned short
2
0 to 65535
Unsigned long integer
unsigned long
4
0 to 4,294,967,295
Single Precision floating-point Number
float
4
1.2E-38 to 3.4E38
(Approx 7 digits precision)
Double precision floating-point number
double
8
2.2E-308 to 1.8E308
(Approx 19 digits precision)


These are the most common sizes we can see, but based on the computer platform, these values may change. But there are certain things according to the ANSI standard which can't be changed. They are
  • The size of char is 1 byte.
  • The size of short is less than or equal to size of int.
  • The size of int is less than or equal to size of long.
  • The size of an unsigned is equal to the size of int.
  • The size of float is less than or equal to the size of a double.

Variable Declarations:

Coming to the declarations of variables. In C we declare the variables by the data type followed by the name. This should end with a semicolon. These data types should be one among the previous mentioned data types and the variable name should follow the rules we have provided in the previous session. If you want you can go back and check the rules in creating a variable name by clicking here.

Syntax:

data_type variable_name;

Examples:
  • int a;
  • float b;
  • char c;
  • int x=3;
We can also initialize the variable while declaring as shown above. Also we can declare the variables of a single data type in a single line.
Ex: int a,b,c;

Note: Declarations in C are allowed only at the top of the block of statements.