data types in c programming int,float,char long ,double, primary data type,secondary data type

format specifies c programming language

3.The float Data Type:
The float data type in c language is used to store all real numbers.
.The range of the real number it can store vries from compiler to compiler.Float data type in C may havee both, the exponential part as well as the decimal part (or the fraction part). It is basically a single-precision type of number.
for 32-bit and 64-bit compiler it is the same as 4 bytes.That is equivalent to 2^(4*8) length of value.
which is 4,29,49,67,296 i.e.0 to 4,29,49,67,296

Float data type in C

#include<stdio.h>
int main()
{
float y = 427.665;
/* by using the suffix f or F */
float x = 1000.5454F;
printf("\n Value of y=%f",y);
printf("\n Value of x=%.2f",y);
}
Output:
Value of y=427.665000
Value of x=1000.55
In our program the first printf("\n Value of y=%f",y);
displays output "Value of y=427.665000."
The %f format specifier is used to print the floating/decimal value of a variable up to the 6 decimal places which is default in c Programming.
The second printf("\n Value of x=%.2f",y); displays output "Value of x=1000.55",
because the format specifier used is %.2f which prints two digits after decimal poit.
The value of variable x=1000.5454F but in output it is 1000.55 because %.2f format specifier rounds it up to two
digits and after two digits the value is 5 so it rounded to 1000.55.
Just like the data type int, we can also use the float data type along with %f modifiers.

4.The double data type
Basically The size of the double data type is 64 bits or 8 bytes. It is capable of storing values that are comparatively double the size of the bytes that the float data type can store. This is the reason why it is known as the double. When looking at the 64 bits in total, the program has 1 bit for the sake of sign representation, the exponent uses 11 bits, and it uses the remaining 52 bits for the mantissa. This data type is capable of holding about 15-17 digits, both after and before the decimal of the data type.

5. Void Data type
Void data type in C language has no meaning and do not have any size.
It is also known as empty data type that is used to declare variable that do not have any type.
Most importantly it is used in defining functions where function do not return any value.

Every data type variable occupies some bytes of memory , it depends on the compiler either it is 32 bit or 64-bit compiler.
Following program demostrate Size of data type in C language

Data types in C language and Size of data type

#include <stdio.h>
int main() {
printf("Size of short int is %2d bytes \n", sizeof(short int));
printf("Size of int is %2d bytes \n", sizeof(int));
printf("Size of long int is %2d bytes \n", sizeof(long int));
printf("Size of signed int is %2d bytes \n", sizeof(signed int));
printf("Size of unsigned int is %2d bytes \n", sizeof(unsigned int));
printf(" \n Size of float is %2d bytes \n", sizeof(float));
printf("Size of double is %2d bytes \n", sizeof(double));
printf("Size of long double is %2d bytes \n", sizeof(long double));
printf("\n Size of signed char is %2d bytes \n", sizeof(signed char));
printf("Size of char is %2d bytes \n", sizeof(char));
printf("Size of unsigned char is %2d bytes \n", sizeof(unsigned char));
return 0;
}
Output:
Size of short int is 2 bytes
Size of int is 4 bytes
Size of long int is 8 bytes
Size of signed int is 4 bytes
Size of unsigned int is 4 bytes
Size of float is 4 bytes
Size of double is 8 bytes
Size of long double is 16 bytes
Size of signed char is 1 bytes
Size of char is 1 bytes
Size of unsigned char is 1 bytes

2. Derived Data Types

A derived data type in C language is basically an combination or aggregation of the primary data type. Examples of primary data types are Void, Float, Integer, and Character. Structures, Unions, Arrays, and Pointers are the derived data types. Derived data types are derived from the primary or fundamental data types.
1.Array:    An array is a collection of elements of the same data type. It used to handle a large amount of data, without the need to declare many individual variables separately. The array elements stored in contiguous memory locations (i.e. one after the other). All the array elements must either any primary data type like int, float, char, double etc., or they can be any user-defined data type like structure and unions. To declare an array, you need to specify the type of the array elements and number of elements.
The syntax is
data_type arrayname[size];

2.Functions:   A function is a group of statements that together perform a task . It allows us to avoid duplicating code that used more than once in Program. Using functions the program, can divide into smaller piece of code parts that are easier for us and others to understand, modify and maintain the program.
Functions can divided into two categories: library functions (in-built functions) and user-defined functions.
Library functions are predefined and precompiled functions that designed to perform some specific tasks.
Example Built in Function is , printf (),scanf (), sqrt (), strlen (), pow() etc.
User-defined functions are the functions which are explicitly defined by the user to meet his requirements. A user-defined function generally created when a user may require to perform some specific task repeatedly, and there is no library function available for performing this task.
Every function in C consists of following components function definition (part of the function where actual code is defined), function prototype (specifies the name of the function, number and type of parameters (if any) and its return type), function call (It used to invoke the function by specifying the function name followed by a list of arguments separating the function name followed by a list of arguments separated by commas enclosed in pair of parentheses).

3.Pointer:  Pointer A pointer is a variable that contains the address of the data items such as variable or function or array rather than a value. It is a derived data type as it built from one of the basic types available in C.
Pointers frequently used in C because of pointer provides efficient techniques for manipulating data in arrays.
function in C programming always return value, to return multiple values from function pointer is used.
Pointers support run type of dynamic memory allocation.
A pointer variable can be declared in the same way as that of normal variable except that an asterisk must precede the name of the pointer variable (*).
The syntax is
data_type *ptr_var_name
For example
int *ptr; Here, ptr is a pointer variable, Which stores the address of any another variable whose data type is int.
Once the pointer variable is declared, we can initialize the pointer variable by assigning the address of some other variable by using the address of operator (&).
For example :
int p = 15;
int *ptr = &p;
A pointer variable can also use to indirectly access the value of the variable whose address stored in it. It is using the dereference operator (*).

3. User Defined Data Types in C

As the name suggests, these data types are created by users using one or more primitive or basic data types in combination.These data types are defined or created by the user as per their need. If a user feels a need of having a data type which is not predefined in C library, then they make their own.
Primitive or basic data types Structure, union, enum type definitions help to define user-defined types.
Structure : Structure is a user-defined data type in C, where we can store values of multiple data types.
For example, if you want to store details of employees of a company, where each employee will be having a name, empno and salary. But managing the data of one employee together (string, int and float variable) for a employee is not possible with any of the data types we have discussed so far. Now for this we use another data type known as Structure.

e.g Structure in C to store employee detail

#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
int empno;
float salary;
};
int main() {
struct Employee emp1;
strcpy(emp1.name, "Raj");
emp1.empno = 1990;
emp1.salary = 40980.99;
return 0;
}


Union: Union is Similar to Structure.
Unions consist of one or more members which may be of different data types just like structures. However, unlike structures where each member is assigned a unique storage area, in the union, all the members share the same storage area within the computer’s memory.
In union the space allocation is equal to the highest memory required by the data type. The e.g is:

Union example in C

#include <stdio.h>
#include <string.h>
union Emp {
float salary;
char name[20];
}e;
int main() {
union Emp e;
e.salary = 20090.034;
strcpy(e.name, "Raj");
return 0;
}
After declaring a union, you can define union variables, so to define a variable of Emp type, write
union Emp e2;


Enum:Enumerated Data Types C provides a special kind of user-defined data type known as enumerated type.
Enum is user defined data type which is used to make program more redable and undersandable to prpgrammer.The main objective of enum is to assign text values to the concerned index value.
The programmer lists the values that a variable of enumerated data type can take.
Now let us consider a statement,
enum countries{America,India,China,Japan,England,Shrilanka};
in the above statement 0 will be assigned to America,1 India,2 china and so on.

#include<stdio.h>
enum countries{America,India,China,Japan,England,Shrilanka};
int main() {
enum countries cnt;
cnt = America;
printf("%d",cnt);
return 0;
}
Output
0

Previous Topic:-->> Operator and Expression || Next topic:-->>Variable and Identifiers