Table of Contents
Basic Data Types in C
C provides a set of basic data types that are used to define variables and handle different kinds of values. These are fundamental building blocks for working with data in C programs.1. int (Integer)
- It is used to store whole numbers (both positive and negative numbers) without decimals.
- Typical size is 4 bytes (depends on system/compiler).
- The range on 32-bit systems is –2,147,483,648 to 2,147,483,647.
int age = 25;
printf("Age = %d", age);
2. float (Floating-Point)
- It is used to store numbers with fractional (decimal) parts.
- Single precision is about 6 decimal digits of accuracy.
- The size is 4 bytes.
float pi = 3.14;
printf(“Value of pi = %.2f”, pi);
3. double (Double Precision Floating-Point)
- It is similar to float, but with double precision, which means higher accuracy.
- It can store up to 15-16 decimal digits of precision.
- The size is 8 bytes.
double largeNumber = 12345.6789012345;
printf("Number = %.10lf", largeNumber);
4. char (Character)
- It is used to store a single character like ‘A’, ‘1’, etc.
- Internally stored as an integer value using the ASCII codes of the characters.
- The size is 1 byte.
char grade = 'A';
printf("Grade = %c", grade);
| Data Type | Size (Typical) | Range/Precision | Example |
|---|---|---|---|
| int | 4 bytes | –2,147,483,648 to 2,147,483,647 | int age = 20; |
| float | 4 bytes | ~6 decimal digits | float pi = 3.14; |
| double | 8 bytes | ~15 decimal digits | double x = 12.3456789; |
| char | 1 byte | –128 to 127 (ASCII) | char grade = 'A'; |
// C program to show data types in C
#include stdio.h
// Main function
int main()
{
// integer
int age = 21;
// floating-point
float height = 5.9;
// character
char grade = 'A';
// double precision
double pi = 3.1415926535;
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
printf("Value of Pi: %.10lf\n", pi);
return 0;
}
Output:
Age: 21
Height: 5.9
Grade: A
Value of Pi: 3.1415926535
Derived Data Types in C
Derived data types are created using the basic data types and allow programmers to work with collections of values or more complex data structures. Here are the main derived data types in C:1. Arrays
An array is a collection of elements of the same data type, stored in contiguous memory locations. These are useful for handling lists of numbers, characters, or other values.Syntax:
Example:data_type array_name[size];
printf("First mark = %d", marks[0]);int marks[5] = {90, 85, 78, 92, 88};
2. Pointers
A pointer is a variable that stores the memory address of another variable. They are powerful for memory management, dynamic memory allocation, and accessing arrays or functions.Syntax:
Example:data_type *pointer_name;
int x = 10;
int *ptr = &x;
printf("Value of x = %d", *ptr); // dereferencing pointer
3. Structures (struct)
Structures allow the grouping of different data types under one name. They are useful when modelling real-world entities. For example, a student with a name, age, and marks.Syntax:
Example:struct struct_name {
data_type member1;
data_type member2;
...
};
struct Student {
char name[20];
int age;
float marks;
};
struct Student s1 = {"Alice", 20, 89.5};
printf("Name: %s, Age: %d", s1.name, s1.age);
5. Unions (union)
Unions are similar to structures, but all members share the same memory location. They are efficient in memory usage, but only one member holds a value at a time.Syntax:
Example:union union_name {
data_type member1;
data_type member2;
...
};
union Data {
int i;
float f;
char c;
};
union Data d;
d.i = 10;
printf("Integer: %d", d.i);
struct vs union
Below are the differences between struct and union in C:| Features | struct | union |
|---|---|---|
| Memory | Each member has a separate memory. | All members share the same memory. |
| Usage | Stores multiple values at once. | Stores one value at a time. |
| Size | Sum of the sizes of members. | Size of the largest member. |
Enumeration Data Types
The enumeration data type is used to define a set of named integer constants. It increases the readability and makes code easier to maintain, especially when working with related values.Syntax:
enum enum_name { value1, value2, value3, ... };
- By default, the first name is assigned the value 0, and each subsequent name increases by 1.
- You can assign custom values manually.
// C program to show enumeration data type
// with default values
#include stdio.h
// Enumeration
enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN };
// Main function
int main()
{
enum Weekday today = WED;
printf("Day number: %d", today);
return 0;
}
Output:
Example 2: Custom ValuesDay number: 2
// C program to show enumeration data type
// with custom values
#include stdio.h
// Enumeration
enum Status { SUCCESS = 1, FAILURE = -1, PENDING = 0 };
// Main function
int main()
{
enum Status s = FAILURE;
printf("Status Code: %d", s);
return 0;
}
Output:
Status Code: -1
Void Data Type
The void data type is a special type that represents the absence of any value. Unlike other data types like int, float, or char, it does not store data directly. Instead, it is used in specific situations where a type is not required or is unknown.
Void as a Function Return Type
When a function does not return any value, its return type is declared as void.
Example:
// C program to show void as a function
// return type
#include stdio.h
void greet()
{
printf("Hello, World!");
}
// Main function
int main()
{
// function call
greet();
return 0;
}
Void as a Function Parameter
When a function takes no arguments, void can be used in the parameter list.
Example:
void display(void) {
printf("No parameters here!");
}
Void Pointers (void *)
A void pointer is a generic pointer that can hold the address of any data type. It must be typecast before dereferencing.
Example:
// C program to show void pointers
#include stdio.h
// Main function
int main()
{
int x = 10;
float y = 5.5;
void *ptr;
ptr = &x;
printf("Value of x = %d\n", *(int*)ptr);
ptr = &y;
printf("Value of y = %.1f", *(float*)ptr);
return 0;
}
Data Type Modifiers
Modifiers can be applied to the basic data types to change their size or range. Modifiers help to optimise memory usage and represent data more precisely. There are four common types of data modifiers:
1. short
- This reduces the size of an integer.
- Typically, 2 bytes instead of 4 bytes.
Example:
short int a = 32767;
printf("%d", a);
2. long
- This increases the size of an integer and sometimes double.
- Typically, 8 bytes for long long int.
long int population = 1000000;
long long int bigNum = 9223372036854775807;
3. signed
- This allows for both positive and negative values.
- This is the default for int and char.
signed int x = -100;
signed char c = -50;
4. Unsigned
This stores only non-negative values.Example:
Unsigned int age = 25;
Unsigned char ch = 255;
| Modifier | Typical Size (bytes) | Range (on 32-bit system) | Example |
|---|---|---|---|
| Short int | 2 | –32,768 to 32,767 | short int x; |
| unsigned short int | 2 | 0 to 65,535 | unsigned short int y; |
| long int | 4 | –2,147,483,648 to 2,147,483,647 | long int z; |
| unsigned long int | 4 | 0 to 4,294,967,295 | unsigned long int n; |
| long long int | 8 | –9 quintillion to +9 quintillion | long long int big; |
| unsigned int | 4 | 0 to 4,294,967,295 | unsigned int u; |
sizeof Operator
The sizeof operator can be used to check the size of any data type.Example:
// C porgram to show sizeof operator
#include stdio.h
// Main function
int main()
{
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu byte\n", sizeof(char));
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Type Conversion in C
Type conversion means changing the data type of one variable to another type. There are two types of type conversion:Implicit Type Conversion
Implicit type conversion is also known as type casting or type promotion by the compiler. It is done automatically by the compiler and converts smaller data types into larger ones to avoid data loss.Conversion Hierarchy:
Example:char-int-float-double
// C program to show implicit conversion
#include stdio.h
// Main function
int main()
{
int x = 10;
float y = 5.5;
// int automatically promoted to float
float result = x + y;
printf("Result = %.2f", result);
return 0;
}
Output:
Result = 15.50
Explicit Type Casting
Explicit type casting is also known as type casting by the programmer. It is performed manually by the programmer using cast operators.Syntax:
Example:(data_type) expression;
// C program to show explicit type conversion
#include stdio.h
// Main function
int main()
{
double pi = 3.14159;
// explicit conversion
int approx = (int) pi;
printf("Approximation = %d", approx);
return 0;
}
Output:
Approximation = 3
Best Practices
Here are some of the best practices for using Data types in C:- Choose Right Data Type:
- Use int for whole numbers, float/double for real numbers, and char for single characters.
- For very large values, prefer long or long long.
- Avoid unnecessary type conversions:
- Cast explicitly where required.
- Mixing types may lead to precision loss.
- Prefer double over float for precision:
- Float is faster but less precise.
- For scientific or financial calculations, double is more precise.
- Initialize Variables Properly: Uninitialized variables may hold garbage values, leading to undefined behavior.
- Use Constants where Applicable: Define values that don’t change with const or #define to improve readability and prevent accidental modification.
Conclusion
In conclusion, understanding C data types is crucial for efficient and accurate programming. They dictate how values are stored and interpreted, from basic integers and floating-point numbers to complex structures and pointers. Choosing the correct data type optimizes memory use and prevents errors, while understanding concepts like type conversion and modifiers ensures robust and reliable code. By following best practices, developers can write clear, maintainable, and highly functional C programs.Frequently Asked Questions
1. What are data types in C?2. Why are data types important in C?Data types in C define the type of value a variable can store, such as integers, characters, or decimal numbers.
3. What are the basic data types in C?They help the compiler allocate the correct amount of memory and ensure that operations on data are performed correctly.
4. What is the difference between float and double?The most common basic data types are int, float, double, and char.
5. What is type conversion in C?float stores decimal numbers with lower precision, while double provides higher precision and can store larger decimal values.
Type conversion is the process of converting one data type into another, either automatically by the compiler or manually using casting.
0 Comments