In this tutorial you will learn about the C Data Types and its application with practical example.
Like any other programming language in C Programming Language “Variables” are one of the vital building blocks of any C data types and program. Variables are reserved memory locations to store values, when we create a variable we are supposed to allocate some memory space for that variable.
The C programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable’s data type and the name of the variable.
Example:
1 |
int sum= 0; |
In C Programming Language every variable must have attached a data type to it, the data type for a variable defines :
- The amount of memory space allocated for variables.
- A data type specifies the possible values for variables.
- The operations that can be performed on variables.
In C Programming Language data types can be broadly classified as :
- Primary data types – int, float, double, char, void
- Derived data types – Derived from primitive data type ex: array, pointer, function, etc.
- User-defined – Defined by the programmer himself.
Primary Data Types In C Language
The C Programming Language supports primary data types. A primary type is predefined by the language and is named by a reserved keyword.
Integer types:
Integers are whole numbers.int is used to define integer numbers.
Syntax:
1 2 3 4 5 |
int <variable name>; Example - int num1; short int num2; long int num3; |
Type | Storage size | Value range |
---|---|---|
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 bytes | 0 to 4,294,967,295 |
Floating Point Types:
The float data type is used to store numbers with a decimal point(real number).
Syntax:
1 2 3 4 5 |
float <variable name>; Example - float num1; double num2; long double num3; |
Type | Storage size | Value range | Precision |
---|---|---|---|
float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places |
Character Type:
Used to define characters. The character type variables can hold a single character.
Syntax:
1 2 3 |
char <variable name>; Example - char ch = ‘a’; |
Type | Storage size | Value range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
Void Type:
The void type means no values. It can not be used with the variable declaration. It is usually used with function to specify its return type or its arguments.
Example:
1 2 3 4 |
void echo_square(int Number) { printf("%d squared is %d\n",Number, Number*Number); } |
User-defined type declaration
C Programming language allows users to create new data types. But it is usually created using an existing data type with another identifier. This user-defined data type identifier can later be used to declare variables. In short, its purpose is to redefine the name of an existing data type.
Syntax:
1 2 3 |
typedef <type> <identifier>; Example - typedef int number; |
Now we can use number identifier instead of int to declare integer variables.
Enum Data Type
This is a user-defined data type having a finite set of enumeration constants or aliases.
Syntax:
1 2 3 |
enum [data_type] {const1, const2, ...., const n}; Example - enum boolean {FALSE=0, TRUE }; |