Data Types in C Programming
In here we will learn about Data types in C Programming . What is Data Type? What is Primary and Derived Data Types in C Programming ? How to use sizeof operator?
C-Data Types:
Data types specify how people enter data into our programs and what type of data is entering.
In C it has some predefined set of data types to handle
different data that we can use in our program.
Each datatype has different storage capacities and ranges.It support
2 data types-
1.Primary data types: This is a fundamental data types in
c like integer(int
), Float(float
), Double(double
),
character(char
) and (void
) types.
2.Derived data types: This data type is nothing but it is combinations of fundamental
data types. Array, union, structure and pointer are Derived data types. .
Primary Data Types
DATA TYPE | MEMORY (BYTES) | FORMAT SPECIFIER |
---|---|---|
short int | 2 | %hd |
unsigned short int | 2 | %hu |
int | 4 | %d |
long int | 4 | %ld |
long long int | 8 | %lld |
unsigned long long int | 8 | %llu |
char | 1 | %c |
float | 4 | %f |
double | 8 | %lf |
long double | 12 | %Lf |
Different data types have different ranges,format specifiers and how much memory it takes to store.This ranges may be different
from compiler to compiler. We can use the sizeof()
operator to check the size of a variable. See the following C program to check out which data types take how much memory:
#include<stdio.h>
int main() {
printf("int takes: %lu bytes.\n",sizeof(int)); // '\n' is for goto new line
printf("float takes: %lu bytes.\n",sizeof(float));
printf("double takes: %lu bytes.\n",sizeof(double));
printf("char take: %lu byte.\n",sizeof(char));
return 0;
}
Output:
int takes: 4 bytes.
float takes: 4 bytes.
double takes: 8 bytes.
char take: 1 byte.