YouTip LogoYouTip

C Data Types

# C Data Types In the C language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. The types in C can be classified as follows: | No. | Type and Description | | --- | --- | | 1 | **Basic Types**: They are arithmetic types and are further classified into: integer types (int), character types (char), floating-point types (float), and double floating-point types (double). | | 2 | **Enumeration Types**: They are also arithmetic types and are used to define variables that can only be assigned certain discrete integer values throughout the program. | | 3 | **Void Type**: The type specifier _void_ indicates that no value is available, and is typically used for function return values. | | 4 | **Derived Types**: They include array types, pointer types, and structure types. | Array types and structure types are collectively referred to as aggregate types. The type of a function specifies the type of the function's return value. In the following sections of this chapter, we will introduce the basic types. The other types will be covered in later chapters. ## Integer Types The following table provides details about the storage size and value range of standard integer types: | Type | Storage Size | Value Range | | --- | --- | --- | | char | 1 byte | -128 to 127 or 0 to 255 | | unsigned char | 1 byte | 0 to 255 | | signed char | 1 byte | -128 to 127 | | int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 | | unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 | | short | 2 bytes | -32,768 to 32,767 | | unsigned short | 2 bytes | 0 to 65,535 | | long | 4 bytes | -2,147,483,648 to 2,147,483,647 | | unsigned long | 4 bytes | 0 to 4,294,967,295 | > Note that the storage size of various types depends on the system architecture, but the commonly used systems today are primarily 64-bit. > > > The following shows the difference in storage size between 32-bit and 64-bit systems (Windows is the same): > > > !(#) To get the exact size of a type or a variable on a particular platform, you can use the **sizeof** operator. The expression _sizeof(type)_ yields the storage size of the object or type in bytes. The following example demonstrates how to get the size of the int type: ## Example #include#includeint main(){printf("int storage size : %lu n", sizeof(int)); return 0; } **%lu** is for a 32-bit unsigned integer. For detailed information, see [C Library Function - printf()](#). When you compile and execute the above program on Linux, it produces the following result: int storage size : 4 ## Floating-Point Types The following table provides details about the storage size, value range, and precision of standard floating-point types: | Type | Storage Size | Value Range | Precision | | --- | --- | --- | --- | | float | 4 bytes | 1.2E-38 to 3.4E+38 | 6 significant digits | | double | 8 bytes | 2.3E-308 to 1.7E+308 | 15 significant digits | | long double | 16 bytes | 3.4E-4932 to 1.1E+4932 | 19 significant digits | The header file float.h defines macros that you can use in your programs along with other details about the binary representation of real numbers. The following example prints the storage space occupied by the floating-point type and its range values: ## Example #include#includeint main(){printf("float storage max bytes : %lu n", sizeof(float)); printf("float minimum value: %En", FLT_MIN); printf("float maximum value: %En", FLT_MAX); printf("precision value: %dn", FLT_DIG); return 0; } **%E** is for outputting single and double precision real numbers in exponential form. For detailed information, see [C Library Function - printf()](#). When you compile and execute the above program on Linux, it produces the following result: float storage max bytes : 4 float minimum value: 1.175494E-38float maximum value: 3.402823E+38precision value: 6 ## Void Type The void type specifies that no value is available. It is typically used in the following three scenarios: | No. | Type and Description | | --- | --- | | 1 | **Function returns void**: There are various functions in C that do not return a value, or you can say they return void. A function with no return value has a return type of void. For example, **void exit (int status);** | | 2 | **Function arguments void**: There are various functions in C that do not accept any parameters. A function with no parameters can accept a void. For example, **int rand(void);** | | 3 | **Pointers to void**: A pointer of type void * represents the address of an object, but not its type. For example, the memory allocation function **void *malloc( size_t size );** returns a pointer to void, which can be converted to any data type. | If you cannot fully understand the void type yet, don't worry too much. We will explain these concepts in detail in subsequent chapters. * * * ## Type Conversion Type conversion is the process of converting a value of one data type to a value of another data type. There are two types of type conversion in C: * **Implicit Type Conversion**: Implicit type conversion occurs automatically in expressions without any explicit instructions or function calls. It typically involves converting a smaller type to a larger type automatically, for example, converting an int type to a long type, or a float type to a double type. Implicit type conversion may also result in data precision loss or data truncation. * **Explicit Type Conversion**: Explicit type conversion requires the use of a type casting operator, which can forcibly convert a value of one data type to another data type. Type casting allows programmers to have more precise control over data types when necessary, but it may also result in data loss or truncation. Implicit type conversion example: ## Example int i =10; float f =3.14; double d = i + f;// Implicitly converts int type to double type Explicit type conversion example: ## Example double d =3.14159; int i =(int)d;// Explicitly converts double type to int type ## 4 Notes Write a Note 1. #0 Justforyou 104***9108@a.com [](#)3600 Common basic data types and their space usage (using a 64-bit machine as an example) * char: 1 byte * int: 4 bytes * float: 4 bytes * double: 8 bytes ## Basic Type Notation Integers * a, defaults to decimal, e.g., 10, 20.
← Angularjs SelectEclipse Jsp β†’