Variables and data types in C

Variables and data types in C

Variables

My favorite description of a variable compares it to a package like a box with a name or label attached to it, for example, “Cotton material” or “Glass wear”, which has something inside, and whatever is inside the box could be clothing or even utensils. So, in Programming generally, we can say variable names are just names for data storage or labels for storing data values just like the label on the box. And just like that in C program variables can be used, changed implemented even more than once.

Data types

Going back to our box illustration, imagine working in a logistics company where you have to group box deliveries according to how fragile they are or what they are used for, or better still how big their sizes are. Data types are similar in that they are the types of data in a variable, and we have four data types in C Program namely:

1. Char : Used to store a single character.

2. Int : Used to store an integers data value

3. Float: Used to store decimals of about 7 digits

4. Double: Used to store decimals of about 15 digits

Each data type reveals the amount of space the variable will occupy like an integer (int) has a storage space of 4 bytes of memory, a single character(char) uses 1byte of memory, Also a float(float) occupies 4 bytes while a double(double) occupies 8 bytes.

Variable declaration

The three stages of variable definition in C are: The declaration where no memory or value is assigned to the variable yet, Then the definition where memory and just a random value are assigned to the variable, and after comes the initialization where you assign a value to the variable yourself.

Note that there are two types of variables the local variable which is declared in a function and the global which is declared outside a function

To declare a variable in C you have this:

data_type variable_name = value
/* During decleartion the data type comes first then the variable name and then its initialized to a value 
* Note how there is no space in between the texts they are replaced with an underscore
*/

Example of data type declarations

#include <stdio.h>

int main() {
    int num = 42; /* integer data type decleration */
    float decimal = 3.14; /* floating-point data type decleration*/
    char letter = 'A'; /* character data type decleration*/
    double big_decimal = 123456789.987654321; /* double precision floating-point data type decleration*/

    printf("Integer num : %d\n", num); /* %d is used to print integers*/
    printf("Floating-point decimal : %f\n", decimal);/* %f is used to print a float*/
    printf("Character letter: %c\n", letter);/*%c is used to print a character */
    printf("Double big_decimal: %lf\n", big_decimal);/*%f OR %fl is used to print a double*/

    return 0;
}

Output

Integer num: 42
Floating-point decimal: 3.140000
Character letter: A
Double big_decimal: 123456789.987654

References

1. https://www.tutorialspoint.com/data-types-in-c

2. https://www.geeksforgeeks.org/variables-in-c/