They're the basic types from which all other data types are derived, serving as the bedrock upon which your code is built. In essence, they're the bread and butter of the C programming world.
Integer Type (int)
An int in C is used to store integer values – numbers without decimal points, like the steps in a stairway or the buildings in a street. They can be both positive or negative.
Size and Range
The size of int usually is 4 bytes (32 bits), allowing it to store values from -2,147,483,648 to 2,147,483,647. However, it's like a one-size t-shirt; it doesn't fit everyone (or every compiler) the same. Its size can vary based on the machine and compiler.
Variants
The int data type has several variations, just like how a tree has branches:
- short int: A smaller version of int, using less memory but also having a smaller range.
- long int: A larger version of int, using more memory but capable of storing larger numbers.
- unsigned int: A positive spin on int, it only deals with non-negative numbers, effectively doubling the highest number it can store.
Floating-Point Type (float)
A float in C is used to store real numbers – numbers with decimal points, like the temperature of a day or the weight of a book.
Size and Range
A float generally occupies 4 bytes of memory, with a range of 1.2E-38 to 3.4E+38. Think of it as a roomy backpack that can hold quite a bit of stuff (or, in this case, numbers).
Precision
However, the float is not as precise as double (another floating-point type) because of the way it's represented in memory. It's like trying to measure a tiny gold nugget with a kitchen scale; you'll get a rough idea, but it won't be precise.
Character Type (char)
The char data type is used to store single characters, like the letters in a sentence or the notes on a music sheet.
Size and Range
A char in C occupies 1 byte of memory, which is enough to store any character from the ASCII table – an encoding standard that includes everything from alphabets and numbers to special characters. It's like a tiny drawer, perfect for storing all your knick-knacks.
Variants
Just like int, char also has an unsigned variant – unsigned char, which only represents positive values.
Long Integer and Double Float Types (long, double)
The long and double types are like the big brothers in the family of int and float, respectively. They can store larger numbers, providing extra room when you need to deal with extensive numeric values. Consider them your trusty 4x4 trucks when your sedan (int or float) just isn't enough.