Beruflich Dokumente
Kultur Dokumente
Introduction
Every programming language deals with some data. For example to print some message or to solve
some mathematical expression some data is necessary. A data type is a set of values and a set of
predefined operations on those values. Data types are primarily used in computer programming, in
which variables are created to store data. Each variable is assigned a data type that determines what
type of data the variable may contain.
C is very rich in data types. C data types are used to identify the type of variable, the type of
return value and the type of parameters used by the function.
We can broadly divide all data type in c into three categories.
Primitive datatypes
Derived Datatypes
User defined Data types
The above mentioned data types are further classified into many. The following GO shows the
further division of C data types.
C
P
r
iD
a
m
it
a
t
i
T
v
y
e
p
e
s
The primitive data types can be classified into three general categories.
Void
Integral
Floating point
Void Type :
The void type is denoted by the keyword void, it means no values and no operations. Void can be
mostly used to designate that a function has no parameters and no return value and also define a
pointer to generic data. It is used in three kinds of situations.
Pointers to void
A pointer of type void * represents the address of an object, but not its type.
For Example void *malloc( size_t size ); - can be explained in pointers topic.
Integral Type : Integral type cannot contain a fractional part, they are whole numbers.
C language has three integral types
1.
2.
3.
Boolean
Character
Integer
Boolean : With release C99 ,C language incorporated a new data type, Boolean which is named after
George Boole , who invented mathematical logic and defined Boolean algebra. A variable of the
primitive data type boolean can have two values: true and false , is stored in memory 0(false) 1 (true)
C used integers to represent the Boolean values a positive or negative numbers. The Boolean type is
referred to the keyword bool.
Syntax : bool variable_name = true / false ;
Example:
bool isPass;
bool isDone = false;
bool isTurnedOn = true;
The header stdbool.h in the C Standard Library was introduced in C99.
For example, in the following code:
TRY IT OUT
# include <stdbool.h>
unsigned char b = 256;
if (b) {
/* do something */
}
Explanation: It evaluates to false if unsigned char is 8 bits wide
TRY IT OUT
#include <stdbool.h>
int main()
{
bool gender[2] = {true, false};
return 0;
}
Explanation : Here, the gender type array which store two values to represent male and female
Character : A character is any value that can be represented in the computers alphabet, is known as
character set. char is a special integer type designed for storing single characters. The integer value
of a char corresponds to an ASCII character. E.g., a value of 65 corresponds to the letter A, 66
corresponds to B, 67 to C, and so on. The char data type is an integral type, always having exactly 8
bits , there are 256 different values in the character set.
Char: key word char is used to represent character, which can store only one character.
Syntax: char variable_name = value ;
Example : char c1= a; (The value stored in represent the value in ascii)
Here, c1 is a variable of type character which is storing a character a.
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, 'A', value=65
For, '2', value=49 (if enclosed in even the integer value is also converted into ASCII value)
For, '&', value=33
TRY IT OUT
#include <stdio.h>
# include < conio.h>
int main()
{
char a;
a=E;
print (value of a= %c, a);
getch();
return 0;
}
Explanation : It declares the variable a and stores the value E in it and finally prints it.
OutPut : value of a = E
Wchar_t : can hold a wide character. It is also required for declaring or referencing wide
characters and wide strings.
wchar.h is a header file in the C standard library to represent wide characters.
Integer Type : Integer data type is used to declare a variable that can store numbers without a decimal.
The keyword used to declare a variable of integer type is int.
Qualifiers : Qualifiers alters the meaning of base data types to give up a new data
type.
The size and range of these data types may vary among processor types and compilers.
Data type qualifiers modify the behavior of variable type to which they are applied. Data type
qualifiers can be classified into two types.two types.
1. size qualifiers
2. sign qualifiers
Size qualifiers:
Size qualifiers alter the size of the basic data types. There are two size qualifiers that can be
applied to integer: short and long.
The minimum size of short int is 16 bit. The size of int must be greater than or equal to that of a short
int. The size of long int must be greater than or equal to a short int. The minimum size of a long int is
32 bits.
Sign qualifiers:
The keywords signed and unsigned are the two sign qualifiers that specify whether a variable
can hold both ve and +ve numbers, or only +ve numbers. These qualifiers can be applied to the data
types int and char only.
Example : unsigned int i; /* which stores the 2 bit +ve number for the variable I */
Signed
Qualifi
er
Size(byt
es)
Range
short int
signed
-128 to 127
Int
unsigne
d
0 to 65535
Int
signed
-32,768 to 32767
signed
-2,147,483,648 to 2,147,483,647
Unsigne
d
0 to 4,294,967,295
signed
-9223372036854775808 to
9223372036854775807
unsigne
d
0 to 18446744073709551615
long int
long int
Declaring the integer : to declare integer data type following syntax should be followed:
Syntax : [qualifier] int variable_name; /* qualifier is optional*/
Keyword int is used for declaring the variable with integer type. The type of qualifier is used to
represent size and sign qualifiers.
Example :
int num1;
short int num2;
long int num3;