In C language, basic data types (also known as primary data types) are the fundamental types provided by the language to represent various kinds of data. These types define the kind of data that can be stored in a variable and dictate the operations that can be performed on that data. Below is an overview of the basic data types in C:
1. Integer (int
)
- Description: Used to store whole numbers (both positive and negative).
- Size: Typically 4 bytes (32 bits), but it can vary based on the system architecture.
- Range: Depends on whether it's signed or unsigned.
- Signed int: to
- Unsigned int: to
- Example:
int age = 25;
2. Character (char
)
- Description: Used to store individual characters (letters, digits, punctuation, etc.).
- Size: Always 1 byte (8 bits).
- Range: to for signed characters, or to for unsigned characters.
- Example:
char initial = 'A';
3. Floating-Point (float
)
- Description: Used to store decimal numbers (floating-point numbers) with single precision.
- Size: Typically 4 bytes (32 bits).
- Range: Approximately to with about 7 decimal digits of precision.
- Example:
float height = 5.9;
4. Double (double
)
- Description: Used to store decimal numbers with double precision, allowing for more precise values than
float
. - Size: Typically 8 bytes (64 bits).
- Range: Approximately to with about 15 decimal digits of precision.
- Example:
double distance = 12345.678;
Modifiers for Basic Data Types
C allows the use of modifiers to change the properties of the basic data types. These modifiers can adjust the size and range of the data types.
Short: Reduces the size of the integer type.
- Example:
short int smallNum = 100; // Typically 2 bytes
- Example:
Long: Increases the size of the integer type.
- Example:
long int largeNum = 100000L; // Typically 4 or 8 bytes
- Example:
Unsigned: Indicates that the variable can only hold non-negative values.
- Example:
unsigned int positiveNum = 500; // Can only store positive integers
- Example:
Example Program Using Basic Data Types
Here’s a simple program that demonstrates the use of various basic data types:
#include <stdio.h>
int main() {
int age = 30; // Integer type
char initial = 'A'; // Character type
float height = 5.9; // Float type
double distance = 1500.75; // Double type
unsigned int score = 85; // Unsigned integer
printf("Age: %d\n", age);
printf("Initial: %c\n", initial);
printf("Height: %.1f\n", height);
printf("Distance: %.2f\n", distance);
printf("Score: %u\n", score);
return 0;
}
Output
Age: 30
Initial: A
Height: 5.9
Distance: 1500.75
Score: 85
Summary
- Basic Data Types: Include
int
,char
,float
, anddouble
, which represent fundamental types of data. - Modifiers: Include
short
,long
, andunsigned
, which can alter the size and range of basic types. - Memory Size: The size of data types can vary based on the system architecture.
Understanding basic data types is essential for effective programming in C, as they form the foundation for declaring and manipulating variables.