Variable Types in C

Embedded C for microcontrollers

In this article, we will explain all the different variable types that the C language has.

Data can be stored in many different ways in a C program.

2 of the ways they can be stored is in variables and constants.

Variables, as in algebra, are values which can be changed in a program. Constants are values which are fixed.

Variables and constants come in many forms and sizes.

Variable Types

A variable is declared by the reserved word indicating its type and size followed by an identiifer.

int people;
char a;
long int groups;

Variables and constants are stored in the limited memory of the microcontroller, and the compiler needs to know how much memory to set aside for each variable without wasting memory space unnecessarily.

Below is a table of all the variable types in C, how many bits they use, and what range (in size) they can cover.

Type Size (Bits) Range (of size)
bit 1 0,1
char 8 -128 to 127
unsigned char 8 0 to 255
signed char 8 -128 to 127
int 16 -32768 to 32767
short int 16 -32768 to 32767
unsigned int 16 0 to 65535
signed int 16 -32768 to 32767
long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
signed long int 32 -2147483648 to 2147483647
float 32 ±1.175e-38 to ±3.402e38
double 32 ±1.175e-38 to ±3.402e38

A bit is a variable that can hold a single digit ,either 0 or 1.

A char is a variable which can hold a single character, such as a-z.

An int is a variable that can hold a whole number digit. It does not allow for decimal points.

Both float and double are variables that allow for decimal points. They both can hold very large numerical values.

Other connotations to watch out for are signed and unsigned. Signed means that the value can be either positive or negative in value. Unsigned means the value can only be positive. It cannot hold a negative value.

Short means a smaller number is being kept in a variable. This allows the program to allocate less memory to the variable, and thus the program can run more efficiently from a memory perspective. Long means a larger number, meaning the variable can store a larger number.

And these are the basics of the different variable types in C.

HTML Comment Box is loading comments...