C Basic Data types
Basic Data types
The basic data types in the C language hold one value at a time. These are also called primitive data types. The basic data types in C language are as follows:
- char
- bool
- int
- float
- double
char
The char data type is used to hold single characters and occupies 1 byte of memory storage.
bool
A bool data type is used to store a boolean value. A boolean value can take one of two values: true or false. The bool data type requires 1 byte of memory.
int
An int data type is used to store whole numbers and cannot store fractional values with a decimal point. There are two variations of this data type:
Short integer
Long integer
The short int requires 2 bytes of memory and the long int requires 4 bytes of memory. For example, we can declare the integers using the following keywords:
short int small_number;
long int large_number;
float
A float data type is used to store numbers that can have fractional values with the decimal point. These numbers are called floating point numbers. The float data type requires 4 bytes of memory.
double
A double data type is used to store floating point numbers with greater precision. The double data type requires 8 bytes of memory. We can use the double data type in calculations that requires greater precision.
Example
The sample demo C program illustrates the size of the data types occupied in the memory. The sizeof() operator is used to calculate the size of the data type. This operator returns the variable’s memory size in bytes.
#include<stdio.h> /* ************************************************************** * Program Description: C Basic Data types * Filename: datatypes.c * C Tutorials - www.TestingDocs.com *****************************************************************/ void main() { // Declare variables of different data types char c; int i; float f; double d; // Print the memory size to hold the variables printf("Character data type size :: %d byte \n",sizeof(c)); printf("Integer data type size :: %d bytes \n",sizeof(i)); printf("Float data type size :: %d bytes \n",sizeof(f)); printf("Double data type size :: %d bytes \n",sizeof(d)); }
—
C Tutorials
C Programming Tutorials on this website:
https://www.testingdocs.com/c-language-tutorial/