What are the basic data types in C?
In C programming, there are several basic data types that are used to store different kinds of values. These data types define the size and nature of the values that can be stored in variables.
Data types in c programming |
Here, we'll discuss the basic data types in C along with their ranges and examples.
1. int: The "int" data type is used to store integer values. It typically occupies 4 bytes of memory and can represent whole numbers within a specific range. The range of "int" data type is approximately -2.1 billion to +2.1 billion.
example:
int age =
25;
2. float: The "float" data type is used to store floating-point numbers (real numbers). It occupies 4 bytes of memory and can store decimal values with a precision of about 6 digits.
example:
float pi =
3.14;
3. double: The "double" data type is similar to "float," but it provides greater precision and occupies 8 bytes of memory. It can store decimal values with a precision of about 15 digits.
example:
double
average = 87.56;
4. char: The "char" data type is used to store individual characters. It occupies 1 byte of memory and can store a single character from the ASCII character set.
example:
char grade
= 'A';
5. short: The "short" data type is used to store integer values that require less memory compared to "int." It typically occupies 2 bytes of memory. The range of "short" data type is approximately -32,768 to +32,767.
example:
short count
= 1000;
6. long: The "long" data type is used to store integer values that require more memory compared to "int." It typically occupies 4 bytes of memory. The range of "long" data type is approximately -2.1 billion to +2.1 billion, similar to "int."
example:
long
population = 7500000;
7. unsigned: The "unsigned" keyword is used with integer data types to represent only positive values. It expands the range of positive values that can be stored.
example:
unsigned
int positiveNumber = 500;
These are
the basic data types in C. It's important to choose the appropriate data type
based on the range and precision required for storing values to optimize memory
usage. Understanding these data types allows programmers to effectively
manipulate and store different types of data in C programs.