Table of Contents
In this lesson, you will learn about data types in C, how they are declared, and the primary data types that every beginner C programmer should know.
C Data Types
In programming, data types are used to specify how the variable or functions are declared. The variable’s data type determines what the variable is about( Number, Character, Decimal) and the size allocated for that variable.
In C, we can classify the datatypes into four types:
Data Types | Usage |
---|---|
Common Data Types | Integer (int) floating-point (float), character ( char), string, and void are commonly used datatype in C. |
Derived Data Types | Derived Data Types are more complex structures of the data types above, such as arrays, structs, pointer, and union. |
Enumeration Data Types | For Enumerators, which will be discussed in detail later in this course. |
Void Data Types | for void variables declaration |
Common Data Types in C
Below are some of the commonly used datatype in C and their characteristics.
-
int
: used to store an integer value. -
float
: to store decimal numbers with single precision. -
double
: to store decimal numbers with double precision. -
char
: to store a single character. -
string
: is used to store a string.
Data Types Characteristics
Type | Size (bytes) | Value range |
---|---|---|
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
char | 1 byte | -128 to 127 or 0 to 255 |
float | 4 bytes | 1.2E-38 to 3.4E+38 |
double | 8 bytes | 2.3E-308 to 1.7E+308 |
short int | 2 bytes | -32,768 to 32,767 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
long int | 4 or 8 bytes | -9223372036854775808 to 9223372036854775807 |
long long int | at least 8 bytes | -(2^63) to (2^63)-1 |
unsigned long int | at least 4 bytes | 0 to 18446744073709551615 |
unsigned long long int | at least 8 bytes | |
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 |
Declaring Data type in C
int
int, or integer, is a number that can be zero or value of a positive or negative number. A variable of int data type cannot contain a decimal value.
To declare an int variable, use the int keyword, followed by the variable name
int number;
You can also declare multiple variables of the same data type for better code organization. Example:
int number1, number2;
float
float is used to store decimal numbers with single precision.
float salary;
double
double is another Data Type that is used to store a decimal number. However double can hold a double-precision float data type.
double price;
char
char is used to declare a variable of type character.
char flag= 'y';
short
short is used to store small integers. Please refer to the table above for short Data Type size.
short x;
long
unlike short, a long keyword is used to store large numbers.
long y;
void
void is used when a Type is not available. For example, a function that does not return a specific Data Type uses the void keyword.
void DoSomething() { printf("I'm a void!"); }
bool
bool is used for variables that have values of true or false. Unlike C++, the bool Data Type in C requires the library stdbool.h
to be included.
"stdbool.h" bool isActive = false;
Enumerated
Enumerated is a user-defined datatype in C. Also know as enum, it’s a set of constants that makes programmers’ life easier, by defining enumerated Data type to be used later in the program. An example of Enumerated Data Type is the Days of the Week enum:
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
In this case, the Day Data Type can be used anywhere in the program as needed.
Another example of Enum is the months of a year:
enum Month { ; Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
signed and unsigned Variables
signed and unsigned are C modifiers. there are used to change the storage of a datatype from going to positive to negative numbers.
For Example, unsigned int will only hold zero and positive numbers.
int y; // Range from -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 unsigned int x; // Range from 0 to 65,535 or 0 to 4,294,967,295
Constants in C
a constant, as the name indicates, is used to hold constant value, such as the PI, or the number of months in a year.
For example:
const double PI = 3.14; const numberOfMonth = 12;
a constant value cannot be changed later in the program. For example, the code below will throw an error
const numberOfMonth = 13;