Summary: in this tutorial, you’ll learn about the C data types, including basic types, derived types, enumeration, and void.
Introduction to the C data types
In C, an object refers to a memory location where its content represents a value. If you assign an object a name, that object becomes a variable.
A data type determines the number of bytes allocated to a variable and valid operations that you can perform.
C provides the basic types, derived types, enumeration type, and void:
Note that this tutorial provides you with an overview of C data types. And you’ll learn each of them in detail in the next tutorials. It’s fine if you don’t understand fully.
Basic types in C
C has some basic types:
char
– a single byte that can hold one character.int
– integer type.float
– single-precision floating-point type.double
– double-precision floating-point type.
char
The char
type can hold a single character. To form a literal character, you use the single quotes ('
). For example:
char ch = 'A';
Code language: C++ (cpp)
int
To represent the integers, C uses the int
keyword:
int n = 100;
C has some qualifiers that can apply to the integers. For example, you can use the short
and long
qualifiers for integers like this:
short int quantity;
long int counter;
Code language: C++ (cpp)
The short
and long
qualifiers change the sizes of integers. The int
keyword can be omitted in the declaration:
short quantity;
long counter;
Code language: C++ (cpp)
The signed
and unsigned
qualifiers may be applied to integers. The unsigned numbers always zero or positive:
unsigned int quantity; // quantity cannot be negative
Code language: C++ (cpp)
float
The float
type stores a single precision floating-point number:
float pi = 3.14;
Code language: C++ (cpp)
double
The double type stores a double-precision floating point number:
double price = 9.99;
Code language: C++ (cpp)
Typically, the number of bytes of a double is as twice as the number of bytes of a float.
Boolean
C doesn’t support the Boolean type natively. Instead, it uses integers to represent boolean values. All non-zero numbers are true
while zero is false
.
To make it more convenient working with boolean values, C defines the bool
and a pair of constants true
and false
in the stdbool.h
standard library. For example:
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool active = false,
status = true;
return 0;
}
Code language: C++ (cpp)
Derived types
The derived types in C are arrays, pointers, struct, and union.
Array
An array is a list of elements with the same type. For example, you can have an array of integers:
int numbers[] = {1, 2, 3};
Code language: C++ (cpp)
Pointer
A pointer is a variable that stores the address of an other variable. Suppose, you have a variable n
:
int n = 0;
Code language: C++ (cpp)
The address of n
is &n
. To define a pointer that stores the address of n
, you use the indirection operator (*):
int *pn = &n;
Code language: C++ (cpp)
By using the pointer, you can manipulate the variable n
indirectly. For example, you can assign a value to n
by using the pointer like this:
*pn = 10;
Code language: C++ (cpp)
The value of n
now is 10
.
Struct
A struct can contain multiple variables of different types. For example, you can define a struct person
that has the first_name
, last_name
, and age
:
struct person
{
char first_name[25];
char last_name[25];
unsigned age;
}
Code language: C++ (cpp)
Enum type
Enumeration is a list of named integer constants. The following example defines a list of statuses: open
, assigned
, and fixed
. They correspond to the integer 1, 2, and 3.
enum STATUS
{
open = 1,
assigned = 2,
fixed = 3
};
enum STATUS bug_status = open;
Code language: C++ (cpp)
void
The void
is an empty type. When a function doesn’t return any value, you can use the void
type. For example:
void say(char* something);
Code language: C++ (cpp)
Summary
- The basic types in C are char,
int
,float
, anddouble
. - C uses integers to represent boolean values. Nonzero numbers are
true
, while zero isfalse
. - Derived types include array, pointer, struct, and union.
- An enumeration is a list of named integer constants.
- The void type is an empty type.