C supports a large number of data types. The built in or basic data types supported by C are integer, floating point and character.
Some commonly used data types are summarized in table along with description.
Type | Description |
---|---|
char | A Single Character |
int | Small integer number |
long int | Large integer number |
float | Small real number |
double | Double precision real number |
long double | Long double precision real number |
The exact sizes and ranges of values for the fundamental types are implementation dependent.
char data type represents a single character. For example, A is a character, and 7 is a number. But a computer can only store numeric code. Therefore, characters such as A, a, B, b, and so on all have a unique numeric code that is used by computers to represent the characters.
You can set the data type of a variable to char by using the following declaration format:
char variablename;
A character enclosed in single quotes (`) is called a character constant. For instance, `A', `a', `B', and `b' are all character constants.
/* This program demonstrates to store character
constants in variable and display it.
*/
#include <stdio.h>
int main()
{
char ch;
ch = 'A';
printf("The value of ch : %c", ch);
return 0;
}
Output:
The value of ch : A
/* This program illustrates how to get character from user
and prints its ASCII value. */
#include <stdio.h>
int main()
{
char letter;
printf("Enter a character :");
scanf("%c", &letter);
printf("ASCII value of %c is %d.", letter, letter);
return 0;
}
Output :
Enter a character :A
ASCII value of A is 65.
You saw the integer data type in previous lesson. The int
keyword is used to specify the type of a variable as an integer.
Integer numbers have no fractional part or decimal point.
The floating-point number is another data type in the C language. Unlike an integer number, a floating-point number contains a decimal point. For instance, 6.31 is a floating-point number; so are 7.1 and -9.14. A floating-point number is also called a real number. A floating-point number is specified by the float
keyword in the C language. You can use the floating-point format specifier (%f) to format your output.
/* This program demonstrates how to use the format specifier %f
with the printf() function for floating point data.
*/
#include <stdio.h>
int main()
{
float number;
printf("Enter a real number: ");
scanf("%f",&number);
printf("The number is %f",number);
return 0;
}
Output :
Enter a real number: 4.71
The number is 4.710000
The process in which one pre-defined type of expression is converted into another type is called conversion. There are two types of conversion in C.
1. Implicit conversion
2. Explicit conversion
Implicit conversion
Data type can be mixed in the expression. For example
double a;
int b = 5;
float c = 8.5;
a = b * c;
When two operands of different type are encountered in the same expression, the lower type variable is converted to the higher type variable. The following table shows the order of data types.
Order of data types | |
Data type long double double float long int char |
order (highest) To |
The int value of b is converted to type float and stored in a temporary variable before being multiplied by the float variable c. The result is then converted to double so that it can be assigned to the double variable a.
Explicit conversion
It is also called type casting. It temporarily changes a variable data type from its declared data type to a new one. It may be noted here that type casting can only be done on the right hand side the assignment statement.
totalPay = (double)salary + bonus;
Initially variable salary is defined as float but for the above calculation it is first converted to double data type and then added to the variable bonus.