Data Types
C has a concept of 'data types' which are used to define a variable before its use. The definition
of a variable will assign storage for the variable and define the type of data that will be held in
the location. The value of a variable can be changed any time. The data types are the core
building blocks in C.
C has the following basic built-in data types.
int
float
char
void
In general the overall data types are used in C/C++ are here:
int (Integer)
unsigned int
long
unsigned long
long long int
float
double
long double
char (Character)
unsigned char
array
pointer
enum (Enumeration)
struct (Structure)
union
Data Types Range
int - data type
int is used to define integer numbers.
{
int Count;
Count = 5;
}
float - data type
float is used to define floating point numbers.
{
float Miles;
Miles = 5.6;
}
The float type has a precision up to 7 digits. The double data type has a precision up to 15
digits.
char - data type
char defines characters.
{
char Letter;
Letter = 'x';
}
void type
void type means no value. This is usually used to specify the type of functions which returns
nothing. Void can be as follows:
void main()
Array
An array is a collection of data items, all of the same type, accessed using a common name.
Array works on integer and character value and can be declared as
type array_name [array_size]
Pointers
A pointer is a variable whose value is the address of another variable, i.e., direct address of the
memory location. Like any variable or constant, you must declare a pointer before using it to
store any variable address. The general form of a pointer variable declaration is as follows:
type *var_name
Enum (Enumeration)
Enumeration is a user defined datatype in C language. It is used to assign names to the integral
constants which make a program easy to read and maintain. The enum is used for values that are
not going to change (e.g., days of the week, colors in a rainbow, number of cards in a deck, etc.
enum day {
Saturday, Sunday, Monday, Tuesday, Thursday, Friday
};
Structure (struct)
Structure is a user-defined datatype in C language which allows us to combine data of different
types together. Structure helps to construct a complex data type which is more meaningful. It is
somewhat similar to an Array, but an array holds data of similar type only. But structure on the
other hand, can store data of any type, which is practical more useful.
struct Student
{
char name[25];
int age;
char branch[10];
char gender;
};
Union
A union is a data type which has all values under it stored at a single address. It is a special data
type available in C that allows storing different data types in the same memory location. You can
define a union with many members, but only one member can contain a value at any given
time. Unions provide an efficient way of using the same memory location for multiple-purpose
union car
{
char name[50];
int price;
}
Difference Between Structure and Union In C:
C Structure C Union
Union allocates one common storage space for all
its members.
Structure allocates storage space for
Union finds that which of its member needs high
all its members separately.
storage space over other members and allocates
that much space
Structure occupies higher memory Union occupies lower memory space over
space. structure.
We can access all members of We can access only one member of union at a
structure at a time. time.
Structure example: Union example:
struct student union student
{ {
int mark; int mark;
char name[6]; char name[6];
double average; double average;
}; };