Data types in C
- Fundamental Data Types
- Integer types
- Floating Type
- Character types
- Derived Data Types
- Arrays
- Pointers
- Structures
- Enumeration
Syntax for declaration of a variable
data_type variable_name;
Integer data types
Keyword int is used for declaring the variable with integer type. For example:
int var1;
Here,
var1 is a variable of type integer.
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you
consider an integer having size of 4 byte( equal to 32 bits), it can
take 2
32 distinct states as: -2
31,-2
31+1, ...,-2, -1, 0, 1, 2, ..., 2
31-2, 2
31-1
Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1. If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -2147483648, program will not run correctly.
Floating types
Variables of floating types can hold real values(numbers)
such as: 2.34, -9.382 etc. Keywords either float or double is used for
declaring floating type variable. For example:
float var2;
double var3;
Here, both
var2 and
var3 are floating type variables.
In C, floating values can be represented in exponential form as well. For example:
float var3=22.442e2
Difference between float and double
Generally the size of float(Single precision float data
type) is 4 bytes and that of double(Double precision float data type) is
8 bytes. Floating point variables has a precision of 6 digits whereas
the the precision of double is 14 digits.
Note: Precision describes the number of significant decimal places that a floating values carries.
Character types
Keyword char is used for declaring the variable of character type. For example:
char var4='h';
Here, var4 is a variable of type character which is storing a character 'h'.
The size of char is 1 byte. The character data type consists of ASCII
characters. Each character is given a specific value. For example:
For, 'a', value =97
For, 'b', value=98
For, 'A', value=65
For, '&', value=33
For, '2', value=49
Qualifiers
Qualifiers alters the meaning of base data types to yield a new data type.
Size qualifiers:
Size qualifiers alters the size of basic data type. The keywords long and short are two size qualifiers. For example:
long int i;
The size of int is either 2 bytes or 4 bytes but, when long
keyword is used, that variable will be either 4 bytes of 8 bytes. Learn
more about
long keyword in C programming. If the larger size of variable is not needed then, short keyword can be used in similar manner as long keyword.
Sign qualifiers:
Whether a variable can hold only positive value or both
values is specified by sign qualifiers. Keywords signed and unsigned are
used for sign qualifiers.
unsigned int a;
// unsigned variable can hold zero and positive values only
It is not necessary to define variable using keyword signed
because, a variable is signed by default. Sign qualifiers can be applied
to only int and char data types. For a int variable of size 4 bytes it
can hold data from -231 to 231-1 but, if that variable is defined unsigned, it can hold data from 0 to 232 -1.
Constant qualifiers
Constant qualifiers can be declared with keyword const. An object declared by const cannot be modified.
const int p=20;
The value of p cannot be changed in the program.
Volatile qualifiers:
A variable should be declared volatile whenever its value
can be changed by some external sources outside program. Keyword
volatile is used to indicate volatile variable.
Comments
Post a Comment