Data Types in C
Data Types in C
Data types specify the type of data that a variable can hold. They determine the size and layout of the variable’s memory, the range of values that can be stored within that memory, and the set of operations that can be performed on the variable. Here is an overview of the primary data types in C:
1. Basic Data Types
int
: Integer data type.- Typically 4 bytes in size.
- Can hold whole numbers, both positive and negative.
int a = 10;
int b = -20;
2.
float
: Floating-point data type.
- Typically 4 bytes in size.
- Used to hold decimal numbers (single precision).
Example:
float pi = 3.14f;
float temperature = -0.5f;
3. double
: Double-precision floating-point data type.
- Typically 8 bytes in size.
- Used to hold decimal numbers with double precision.
Example:
double e = 2.718281828459045;
double largeValue = 123456.7890123456789;
4.
char
: Character data type.
- Typically 1 byte in size.
- Used to hold a single character.
Example:
char letter = 'A';
char symbol = '#';
2. Derived Data Types
array
: Collection of elements of the same type.
Example:
int numbers[5] = {1, 2, 3, 4, 5};
char name[10] = "Alice";
2.
pointer
: Stores the address of another variable.
Example:
int a = 10;
int *ptr = &a;
3.
struct
: User-defined data type that groups related variables of different types.
Example:
struct Person {
char name[50];
int age;
float height;
};
struct Person person1 = {"John Doe", 30, 5.9f};
4.
union
: Similar to a struct
, but members share the same memory location.
Example:
union Data {
int i;
float f;
char str[20];
};
union Data data;
data.i = 10;
5.
enum
: Enumerated type, which consists of named integer constants.
Example:
enum Weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum Weekday today = Wednesday;
3. Void Data Type
void
: Used to specify that no value is available.- Commonly used in functions to indicate they do not return a value.
void printMessage() {
printf("Hello, World!\n");
}
4. Type Modifiers
Type modifiers are used to alter the properties of basic data types, like their size and range.
short
: Typically reduces the size of anint
.
Example:
short int smallNumber = 32767;
2.
long
: Typically increases the size of an int
.
Example:
long int largeNumber = 2147483647;
3.
signed
: Allows a variable to store both positive and negative values.
Example:
signed int value = -100;
4.
unsigned
: Allows a variable to store only positive values (including zero).
Example:
unsigned int positiveValue = 100;
Summary of Data Types with Sizes and Ranges
Here’s a summary of some of the common data types, their typical sizes, and their ranges:
Data Type | Size (bytes) | Range |
---|---|---|
char | 1 | -128 to 127 (signed), 0 to 255 (unsigned) |
int | 4 | -2147483648 to 2147483647 (signed), 0 to 4294967295 (unsigned) |
short int | 2 | -32768 to 32767 (signed), 0 to 65535 (unsigned) |
long int | 4 or 8 | -2147483648 to 2147483647 (4 bytes) or longer range for 8 bytes |
long long int | 8 | -(2^63) to (2^63)-1 (signed) |
float | 4 | 1.2E-38 to 3.4E+38 |
double | 8 | 2.3E-308 to 1.7E+308 |
long double | 10, 12, or 16 | 3.4E-4932 to 1.1E+4932 |
Example Program Using Various Data Types
#include <stdio.h>
int main() {
char character = 'A';
int integer = 100;
float floatingPoint = 3.14f;
double doublePrecision = 2.718281828459045;
short int shortInteger = 32767;
long int longInteger = 2147483647;
unsigned int unsignedInteger = 4294967295;
printf("Character: %c\n", character);
printf("Integer: %d\n", integer);
printf("Float: %f\n", floatingPoint);
printf("Double: %lf\n", doublePrecision);
printf("Short Int: %hd\n", shortInteger);
printf("Long Int: %ld\n", longInteger);
printf("Unsigned Int: %u\n", unsignedInteger);
return 0;
}