Data Types in C:
*Each variable in C has an associated data type.
*Each data type requires different amounts of memory and has some specific operations which can be performed over it.
*Data types specify how we enter data into our programs and what type of data we enter.
*C language has some predefined set of data types to handle various kinds of data that we can use in our program.
*These datatypes have different storage capacities.
*In C programming, data types are declarations for variables.
*This determines the type and size of data associated with variables.
C language supports 2 different type of data types:
These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.
Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointer.
*void type
- void type means no value.
- This is usually used to specify the type of functions which returns nothing.
- We will get acquainted to this datatype as we start learning more advanced topics in C language, like functions, pointers etc.
*char:
- The most basic data type in C.
- It stores a single character and requires a single byte of memory in almost all compilers.
*int:
- As the name suggests, an int variable is used to store an integer.
*float:
- It is used to store decimal numbers (numbers with floating point value) with single precision.
*double:
- It is used to store decimal numbers (numbers with floating point value) with double precision.
We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:
#include <stdio.h>
int main()
{
int a = 1;
char b ='G';
double c = 3.14;
printf("Hello World!\n");
//printing the variables defined above along with their sizes
printf("Hello! I am a character. My value is %c and "
"my size is %lu byte.\n", b,sizeof(char));
//can use sizeof(b) above as well
printf("Hello! I am an integer. My value is %d and "
"my size is %lu bytes.\n", a,sizeof(int));
//can use sizeof(a) above as well
printf("Hello! I am a double floating point variable."
" My value is %lf and my size is %lu bytes.\n",c,sizeof(double));
//can use sizeof(c) above as well
printf("Bye! See you soon. :)\n");
return 0;
}
OUTPUT:
Hello World!
Hello! I am a character. My value is G and my size is 1 byte.
Hello! I am an integer. My value is 1 and my size is 4 bytes.
Hello! I am a double floating point variable. My value is 3.140000 and my size i
s 8 bytes.
Bye! See you soon. :)