Data Types
Lesson 7Author : Afrixi
Last Updated : January, 2023
In C programming, data types are used to define the type of data that a variable or function can hold. The C language has several built-in data types, including:
Integer data types
These are used to hold integer values. There are several integer data types in C, including char
, short
, int
, long
and long long
. The number of bits used to represent these types can vary depending on the platform and compiler, but they all represent integer values.
#include <stdio.h>
int main() {
// declare and initialize integer variables
char my_char = 'a';
short my_short = 10;
int my_int = 100;
long my_long = 1000;
long long my_long_long = 10000;
// print the values of the integer variables
printf("my_char = %c\n", my_char);
printf("my_short = %d\n", my_short);
printf("my_int = %d\n", my_int);
printf("my_long = %ld\n", my_long);
printf("my_long_long = %lld\n", my_long_long);
return 0;
}
In this example, we declare and initialize variables of different integer data types (char
, short
, int
, long
, long long
) and then print their values to the console using printf()
function.
Floating-point data types
These are used to hold floating-point values, which are numbers with a fractional part. The two main floating-point data types in C are float
and double
. float
is a single-precision floating-point type that can hold up to 6 decimal digits, while double
is a double-precision floating-point type that can hold up to 15 decimal digits.
#include <stdio.h>
int main() {
// declare and initialize floating-point variables
float my_float = 3.14;
double my_double = 3.14159265358979323846;
// print the values of the floating-point variables
printf("my_float = %f\n", my_float);
printf("my_double = %lf\n", my_double);
return 0;
}
In this example, we declare and initialize variables of the floating-point data types (float
and double
) and then print their values to the console using printf()
function.
Character data type
This is used to hold a single character value. In C, the char
data type is used to represent characters. A char
variable is 1 byte in size and can hold a single ASCII character.
#include <stdio.h>
int main() {
// declare and initialize character variable
char my_char = 'a';
// print the value of the character variable
printf("my_char = %c\n", my_char);
return 0;
}
In this example, we declare and initialize a variable of the character data type (char
) and then print its value to the console using printf()
function.
Boolean data type
This is used to hold Boolean values, which are either true or false. In C, the _Bool
data type is used to represent Boolean values. C also defines a set of macros that can be used to represent true and false values, which are true and false.
#include <stdio.h>
#include <stdbool.h> // include the bool data type and macros
int main() {
// declare and initialize boolean variable
bool my_bool = true;
// print the value of the boolean variable
printf("my_bool = %d\n", my_bool);
return 0;
}
In this example, we declare and initialize a variable of the Boolean data type (bool
) and then print its value to the console using printf()
function.
Void data type
This is a special data type that represents the absence of a value. A void pointer can point to any data type, and a void function returns no value.
#include <stdio.h>
// declare a void function that takes no arguments and returns no value
void my_void_function() {
printf("This is a void function.\n");
}
int main() {
// call the void function
my_void_function();
return 0;
}
In this example, we declare a void function that takes no arguments and returns no value, and then call it from the main function. When the void function is called, it simply prints a message to the console.
In addition to these built-in data types, C also provides the ability to create user-defined data types using structures and unions. A structure is a collection of variables of different data types that are grouped together under a single name, while a union is a collection of variables that share the same memory location.