In this lesson, you will learn about Data Types in C, their usage, and examples to better understand the topic.
Datatypes represent the variable in C programming, which defines the size and type of the variable. This means how much space a variable requires in-memory storage.
Example
int myFirstVariable;
myFirstVariable
is a variable of int (integer) type, and the int size is 4 bytes.
C supports fundamental data types given below in detail. C uses these data types to construct variables.
These are the basic data types: character, integer, float, and void. These basic data types can be classified as signed, unsigned, short, or long.
The integers are the non-decimal data type that includes a number set of {…….., -2, -1, 0, 1, 2, …….}.
Rules for integers are universally the same also in programming languages:
int variable_name;
Example
int mark;
mark is the variable of the integer type
In C programming, signed and unsigned are used as a type modifiers. Data storage can be changed using them.
Example
// Example of integer short long double signed and unsigned #include <stdio.h> int main() { short intShortVar; long intLongVar; long long LongLongVar; long double LongDoubleVar; unsigned int unsignedPosInt = 90; int signedPosInt = 90; int signedNegInt = -90; printf("short size : %d bytes\n", sizeof(intShortVar)); //2 bytes printf("long size: %d bytes\n", sizeof(intLongVar)); //8 bytes printf("long long size : %d bytes\n", sizeof(LongLongVar)); //8 bytes printf("long Double size: %d bytes\n", sizeof(LongLongVar)); //8 bytes printf("unsigned int size : %d bytes\n", sizeof(unsignedPosInt)); //4 bytes printf("signed positive size: %d bytes\n", sizeof(signedPosInt)); //4 bytes printf("signed negative size : %d bytes\n", sizeof(signedNegInt)); //4 bytes return 0; }
Output
short size: 2 bytes long size: 8 bytes long long size : 8 bytes long Double size: 8 bytes unsigned int size : 4 bytes signed positive size: 4 bytes signed negative size : 4 bytes
In C programming, the char type is the fundamental data type used to hold a single character. However, It has the further classification of signed and unsigned. Signed char ranges from 0 to 255, whereas signed char ranges from -128 to 127.
char variablename;
Example
// Example of signed and unsigned character #include <stdio.h> int main() { char myfirstchar = 'q'; unsigned char unsignedchar = 'p'; printf("char size:%d byte\n", sizeof(myfirstchar)); //ranges 0 o127 bits printf("char size:%d byte\n", sizeof(unsignedchar)); //-127 to 127 bits printf("Value of myfirstchar %c\n", myfirstchar); printf("value of unsignedchar %c", unsignedchar); return 0; }
Output
char size:1 byte char size: 1 byte Value of myfirstchar q value of unsignedchar p
Doubles and float data types are floating-point numbers and can contain positive and negative (+ and -) sign fractional or decimal numbers. Double variables are 8 bytes, whereas float is 4 bytes in size. This is the only difference between float and double.
float variable_name; double variable_name;
Example
float marks; double marks;
// Example of double and float #include <stdio.h> int main() { double PI = 3.14; float PII = 3.14; printf("double size in %d byte|||||||||||", sizeof(PI)); printf("PI value is %lf \n", PI); printf("float size in %d byte|||||||||||", sizeof(PII)); printf("PII value is %f \n", PII); return 0; }
Output
double size in 8 byte|||||||||||PI value is 3.140000 float size in 4 byte|||||||||||PII value is 3.140000
Void types return no value. Remember, in C; we cannot create variables of void type. If the function is not returning anything, it has a void return type.
C programming language support derived Data Types: Example arrays, unions, pointers, structures.
We will learn about derived types in later chapters.
Below is a table of Data types in C, their size, and their range.
Data Type | Size | 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 | 8 bytes or 4bytes for 32 bit OS | -9223372036854775808 to 9223372036854775807 |
unsigned long | 8 bytes | 0 to 18446744073709551615 |
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
In the next lesson, you will learn about Operators and their usage in the C programming language.