C Basic2
C Basic2
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
{
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 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)
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
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.