UNDERSTANDING DATA TYPES AND VARIABLES IN C PROGRAMMING

1582C112-1D98-451B-8BAF-A929102F2871.png
Designed using canva

When discussing variables in C programming, it is always necessary to discuss the data types available in C. This is critical so that we can properly assign data to variables based on their data type. The data types available in C and the values that can be allocated to them are listed below.

Data Types
Storage Size
Values assigned
int (integer)
4 bytes
+ive or -ive values btw 32,767
char
1 byte
values like letters or symbols
float
4 bytes
Decimal values
double
8 bytes
Higher Decimal Values
void
0 byte
Simply void (Nothing)

With this table, you can choose which data type to use when assigning data to a variable. There is more to data types than what is shown in the table above, as we still have data type modifiers.

Data type modifiers allow us to specify what type of data is being assigned. When assigning or declaring a variable, you can call it providing a well-descriptive characteristic of the data.

I've been talking about variables but haven't clarified what they mean.

A variable is a term given to hold specific data, i.e. only one data. A variable cannot hold more than one piece of data. Consider it as a box that can only hold one shirt with the name myShirt printed on it.

When naming a variable, variables must be very descriptive and follow naming conventions. The rules that must be fulfilled are as follows:

  • A variable name cannot begin with a number
  • It has to begin with a small (lowercase) letter or an underscore i.e. myShirt, _myShirt
  • it can have both lowercase and uppercase letters i.e. myShirT
  • it should be well descriptive and not more than 31 characters

As I said earlier on Data modifiers:

Data type modifiers allow us to specify what type of data is being assigned. When assigning or declaring a variable, you can call it providing a well-descriptive characteristic of the data.

Examples of Data modifiers are: short int, long int, long long int, unsigned int, signed int and many more. You can read more on this here and here.

We also have what is known as Format Specifiers. This allows the computer to determine which data type is being printed or compiled. As a result, when printing the data, the format specifier for each data type must be written.

Below shows the format specifiers we have for certain data types:

Data Type
Format Specifier
char
%c
int
%d or %i
float
%f
double
%lf

Let's take an example of all that we have learnt... it's time to code!

Remember in the last article I wrote on C programming, I used a similar syntax while writing "hello world". This is because any code written in C always has this syntax.

#include

int main(void)
{
    /* Write in your code */ (How to make comments in C programming)
    
    /* This is an example of 
       Multi line comments */

    return (0);
}

Now, Let's put this syntax into play

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 10_08_25 PM.png
Exp1

In the screenshot above, you can see me assigning values to variables based on their data types... The green colors represent data types, the grey colors represent variable names, and the purple colors represent the data associated with the variable names.

The image above is only an example of how to print the integer variable. As you can see, I used the format specifier and followed it with the integer name. This format is usually used when publishing a variable data type.

printf("formatSpecifier", variableName);

Note: The "\n" symbol only ensure a new line after the output has been printed.

Now, let's see the output of the code written above...

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 10_43_40 PM.png

After compiling, we can see that the output of the code is given as "The number is 3" βœ…...

Let's us print the next example....

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 10_51_56 PM.png
Exp2

In the code above, I inserted the format specifier of the char data type, %c, to let the computer know we're printing a char keyword, followed by the variable name separated by a comma.

Let us see the output of the code below:

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 10_59_14 PM.png

It is also vital to understand that when assigning a value to a variable character, it must be separated by two apostrophes ' ' as seen in the images Exp1 and Exp2. The symbol '-' has been printed.

Let us print the next example...

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 11_13_10 PM.png

To print a float data type, as shown in the image above, you must use the format specifier to indicate that the data type is a float. As a result, %f is used.

Let see the output of the code below:

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 11_18_13 PM.png

I'm sure you're wondering what the difference between a float and a double is. Both can store decimal values, but double is preferable since it can store greater decimal values than float.

Let us take a look at double data type:

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 11_24_07 PM.png

I'm sure you'd guessed by now that the %lf format specifier was on its way. As a result, the computer understands that the variable num2 is a double.

Let us see the output of the code below:

vagrant@ubuntu-focal_ ~_C-programming 9_13_2022 11_28_37 PM.png

Now there you have it. The output of the code...

I believe you would have learned the following by now based on all this article has covered:

  • What is a variable?
  • What are format specifiers?
  • What are Data types?
  • What are Data modifiers?
  • What are the Variable conventions we have for C?
  • How to make comments in C language?
  • How to use the "printf()" function to print a variable?
  • The common syntax in which all codes in C is built..
  • The storage sizes of some data types...
  • The type of values assigned to certain data types.

WHO IS STARSTRINGS01


image.png
Designed by @ grisvisa

Starstrings01, also known as Giftedhands, attends the Federal University of Agriculture in Abeokuta to study Mechatronics Engineering. He is a lover of the hive, a guitarist from Nigeria, and a student.

His ambition on Hive is to be more than just an ordinary blogger; he wants to be someone with a purpose. That's why he started the newbies initiative @newbies-hive to help guide and support newbies. Please follow the @newbies-hive curation trail by clicking here.

He tries to juggle education with being active on the chain, but his love and passion for Hive keep him on track..


All images are mine except indicated otherwise


54TLbcUcnRm3sWQK3AJf6fuxkTxiKXRNCarffscTjF9JnBqLzj89NH5s1rKH2Cga4QvMPvvEcb4koTvuCHZXnviw8k7xAcqd9HsbFKjw6hj1Y72M87h86FHy9hno4Dmynf1K26XEe.png

IF YOU LOVE THIS POST, YOU CAN KINDLY SUPPORT IT WITH YOUR COMMENTS, REBLOG AND UPVOTE. IT WOULD BE MUCH APPRECIATED

54TLbcUcnRm3sWQK3AJf6fuxkTxiKXRNCarffscTjF9JnBqLzj89NH5s1rKH2Cga4QvMPvvEcb4koTvuCHZXnviw8k7xAcqd9HsbFKjw6hj1Y72M87h86FHy9hno4Dmynf1K26XEe.png

THANKS FOR VISITING MY BLOG πŸ˜‡πŸ’•

H2
H3
H4
3 columns
2 columns
1 column
24 Comments
Ecency