Sie sind auf Seite 1von 7

C Data types

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

Primitive data types:


The primitive data types in c language are the inbuilt data types provided by the c language itself.
Thus, all c compilers provide support for these data types For example, integer, character, etc are all
primitive data types. Programmers can use these data types when creating variables in their
programs.
For example, a programmer may create a variable called "rollno" and define it as a integer data type.
The variable will then store data as a integer values.

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.

Function returns as void


There are many functions in C which do not return any value or we can say no return type. The functions
has the return type as void.
For example- void msg (void); - can be explained in functions chapter

Function arguments as void


There are various functions in C which do not take any parameter. A function with no parameter can take
a void.
For example - int average(void);- can be explained in functions chapter

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.

The C standard provides two character types.


Char
Wchar_t

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.

Example : long int i; /* which stores the integer value in 4 bytes.

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 */

Type & size


qualifier

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

long long int


long 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;

long long int num 4 ;


Points to remember
A data type is a set of values and a set of predefined operations on those values.
C language has defined with set of data types.
When we use a variable in a program then we have to mention the type of data. This can be defined
using data type in C.
In C, a variable should be declared before it can be used in program.
Data types are the keywords, which are used for assigning a type to a variable.
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.

Das könnte Ihnen auch gefallen