C is a statically-typed language, which means that the data type of a variable is known at compile time. The language provides a variety of data types to store different types of data. In this section, we will discuss the different data types available in C.
Primary Data Types
The primary data types in C are the basic data types that are built into the language. These data types are:
Integer Types
Integer types are used to store whole numbers. The following are the integer types available in C:
int
: This is the most commonly used integer type. It is a 32-bit signed integer type that can store values ranging from -2147483648 to 2147483647.short
: This is a 16-bit signed integer type that can store values ranging from -32768 to 32767.long
: This is a 32-bit signed integer type that can store values ranging from -2147483648 to 2147483647.long long
: This is a 64-bit signed integer type that can store values ranging from -9223372036854775808 to 9223372036854775807.
Floating-Point Types
Floating-point types are used to store decimal numbers. The following are the floating-point types available in C:
float
: This is a 32-bit floating-point type that can store values ranging from 1.17549435e-38 to 3.40282347e+38.double
: This is a 64-bit floating-point type that can store values ranging from 2.2250738585072014e-308 to 1.7976931348623157e+308.long double
: This is an 80-bit or 128-bit floating-point type that can store values ranging from 3.36210314311209350626e-4932 to 1.18973149535723176502e+4932.
Character Types
Character types are used to store single characters. The following are the character types available in C:
char
: This is an 8-bit signed integer type that can store values ranging from -128 to 127.unsigned char
: This is an 8-bit unsigned integer type that can store values ranging from 0 to 255.signed char
: This is an 8-bit signed integer type that can store values ranging from -128 to 127.
Derived Data Types
Derived data types are data types that are derived from the primary data types. The following are the derived data types available in C:
Array Types
Array types are used to store collections of values of the same type. An array is declared by specifying the type of the elements and the number of elements.
int scores[5]; // declares an array of 5 integers
Pointer Types
Pointer types are used to store memory addresses. A pointer is declared by specifying the type of the value that the pointer points to.
int *ptr; // declares a pointer to an integer
Structure Types
Structure types are used to store collections of values of different types. A structure is declared by specifying the type of each member and the name of the structure.
struct Person {
int age;
char name[20];
}; // declares a structure with two members
Union Types
Union types are used to store values of different types in the same memory location. A union is declared by specifying the type of each member and the name of the union.
union Data {
int i;
float f;
}; // declares a union with two members
Enum Types
Enum types are used to store values that have a specific set of named values. An enum is declared by specifying the name of the enum and the values that it can take.
enum Color {
RED,
GREEN,
BLUE
}; // declares an enum with three values
Void Type
The void type is used to specify that a function does not return a value. It is also used to declare a pointer that can point to any type of value.
void printHello(); // declares a function that does not return a value
void *ptr; // declares a pointer that can point to any type of value
Type Qualifiers
Type qualifiers are used to modify the behavior of a type. The following are the type qualifiers available in C:
Const Qualifier
The const qualifier is used to specify that a variable cannot be modified.
const int x = 5; // declares a constant integer
Volatile Qualifier
The volatile qualifier is used to specify that a variable can be modified by external factors.
volatile int x; // declares a volatile integer
Restrict Qualifier
The restrict qualifier is used to specify that a pointer is the only pointer that points to a particular object.
int *restrict ptr; // declares a restricted pointer
Static Qualifier
The static qualifier is used to specify that a variable is initialized only once.
static int x = 5; // declares a static integer
Auto Qualifier
The auto qualifier is used to specify that a variable is automatically allocated and deallocated.
auto int x; // declares an automatic integer
Register Qualifier
The register qualifier is used to specify that a variable should be stored in a register.
register int x; // declares a register integer
Type Conversions
Type conversions are used to convert a value of one type to another type. The following are the type conversions available in C:
Implicit Conversions
Implicit conversions are conversions that are performed automatically by the compiler.
int x = 5;
double y = x; // implicitly converts x to a double
Explicit Conversions
Explicit conversions are conversions that are performed using a cast operator.
int x = 5;
double y = (double)x; // explicitly converts x to a double
Conclusion
In conclusion, C provides a variety of data types to store different types of data. The primary data types include integer, floating-point, and character types. The derived data types include array, pointer, structure, union, and enum types. Type qualifiers are used to modify the behavior of a type, and type conversions are used to convert a value of one type to another type.
Comments
Post a Comment