2.5 DATA TYPES
Programming in C
DATA TYPES IN C
The type or data type of a variable determines the set of values that the variable can take and the set of operations that can be applied to those values. Every variable in C must be associated with a data type, which informs the compiler about the memory requirement, range of values, and valid operations.
Data types in C can be broadly classified as shown in Fig. 8.7.
Classification of Data Types in C
C data types are classified into the following categories:
1. Primitive / Basic Data Types
-
char
-
int
-
float
-
double
-
void
2. Derived Data Types
-
Array
-
Function
-
Pointer
3. User-Defined Data Types
-
Structure
-
Union
-
Enumeration
4. Valueless Data Type
-
void
C provides a standard minimal set of basic (primitive) data types, from which more complex data types can be constructed.
Basic Data Types in C
C has five basic data types, as shown in Fig. 8.8:
| Data Type | Keyword |
|---|---|
| Character | char |
| Integer | int |
| Floating-point | float |
| Double precision floating-point | double |
| Valueless | void |
Sizes and Ranges of Basic Data Types
The size and range of data types depend on the machine architecture.
For a 16-bit Computer
-
char → 8 bits (–128 to 127)
-
int → 16 bits (–32768 to 32767)
-
float → 32 bits (1.17549 × 10⁻³⁸ to 3.40282 × 10³⁸)
-
double → 64 bits (2.22507 × 10⁻³⁰⁸ to 1.79769 × 10³⁰⁸)
-
void → valueless
For a 32-bit Computer
-
char → 8 bits (–128 to 127)
-
int → 32 bits (–2147483648 to 2147483647)
-
float → 32 bits
-
double → 64 bits
-
void → valueless
According to IEEE standards, the precision of:
-
float ≈ 6 digits
-
double ≈ 15 digits
Void Data Type
The void data type specifies an empty set of values.
It has no values and only one operation, i.e., assignment.
-
Used as the return type of functions that do not return any value
-
Does not refer to any object
-
Is an incomplete type
According to ISO/IEC:
“The void type comprises an empty set of values; it is an incomplete type that cannot be completed.”
Type Modifiers and Qualifiers
C provides:
-
4 type specifiers (modifiers)
-
3 type qualifiers
Important Rules
-
Modifiers apply mainly to int
-
signed and unsigned can be applied to char
-
long can also be applied to double
-
If the base type is omitted, int is assumed
-
void has no modifiers
Type Specifiers and Qualifiers Classification
1. Size Specifiers
-
short
-
long
2. Sign Specifiers
-
signed
-
unsigned
3. Type Qualifiers
-
const
-
volatile
-
restrict
Size Specifiers (short and long)
The size specifiers alter the size of the base data type.
Rules for Integer Sizes
-
Minimum size of short int → 2 bytes
-
int ≥ short int
-
long int ≥ int
-
Minimum size of long int → 4 bytes
Typical Sizes
-
16-bit systems:
short int = int = 2 bytes, long int = 4 bytes -
32-bit systems (gcc):
short int = 2 bytes, int = long int = 4 bytes
The long qualifier is also applicable to double.
C99 Extended Integer Types
C99 introduced:
-
long long int
-
unsigned long long int
These occupy at least 64 bits.
Type Qualifiers
const
-
Makes a variable read-only
-
Value must be assigned at declaration
-
Enables compiler optimization
Example:
Note:
constdoes not make a variable a constant; it only prevents modification through that variable.
volatile
-
Indicates a variable that can change unexpectedly
-
Commonly used for hardware-linked variables
-
Prevents compiler optimization that may remove required memory access
restrict
-
Helps compilers generate faster executables
-
Mainly useful in performance-critical programs
Character Data Type (char)
A char variable occupies one byte and stores the ASCII code of characters.
-
'A'→ ASCII 65 -
'a'→ ASCII 97
C treats char as a sub-range of int.
Thus, characters can be used in arithmetic expressions.
Signedness of char
-
Default signed char range: –128 to 127
-
unsigned char range: 0 to 255
Unsigned char is useful for portable programs and non-character data storage.
Integer Data Types
Signed Integer Types
-
short
-
int
-
long
-
long long
Most systems use 2’s complement representation.
2’s Complement Rules
-
Leftmost bit is the sign bit
-
Negative numbers are obtained by:
-
Complementing bits
-
Adding 1
-
Other encodings (1’s complement, sign-magnitude) are also permitted.
Unsigned Integer Types
-
Use straight binary representation
-
Range: 0 to 2ⁿ – 1
-
Frees the sign bit for magnitude
Example use cases:
-
Memory addresses
-
Population counts
-
Stadium seating capacity
Floating-Point Data Types
float
-
4 bytes
-
Approx. 7 significant digits
-
Used for fractional numbers
⚠ Floating-point values are approximations, not exact values.
double
-
8 bytes
-
15 digits precision
-
Used in scientific and engineering computations
-
Provides larger range and higher accuracy
Boolean Data Type (_Bool)
Introduced in C99.
-
Stores only 0 (false) or 1 (true)
-
Any non-zero value becomes 1
-
Header file
<stdbool.h>defines:-
bool
-
true
-
false
-
Conclusion
Thus, data types in C define how data is stored, interpreted, and manipulated. Proper understanding of basic data types, modifiers, and qualifiers is essential for writing efficient, portable, and error-free C programs, especially for large-scale and system-level applications.
📖 Reference
The content for this subject is prepared by referring to the standard textbook
“Computer Fundamentals and Programming in C”
by Pradip Dey and Manas Ghosh,
Second Edition, Oxford University Press (2018).
The explanation is exam-oriented and strictly aligned with the textbook.
💙 Footer
Aivette-COI is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.
With love and care,
by Aivette 💙
Comments
Post a Comment