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<stdio.h>

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 😇💕



0
0
0.000
24 comments
avatar

Nice post. This is helpful for those who wants to learn programming
I haven't done C programming for decades. I also see that you are using Ubuntu for OS.

0
0
0.000
avatar

Yeah, I am running Ubuntu terminal on my windows system... I am using vagrant Ubuntu to do this!

0
0
0.000
avatar

/* Write in your code */ (How to make comments in C programming)

You can also use // for single line comments. I find this a lot easier than the /* comment */ pattern for single line.

0
0
0.000
avatar

Yeah that too is there... I forgot to mention about the short commands to make comments... Ctrl + / & Ctrl + shift + /..

Particularly when using a Linux editor, I haven't tried the commands.. but // method is not accepted that's why I unconsciously skipped it.

0
0
0.000
avatar

What editor do you use that doesn't accept it?

Vim accepts it.
ss.png

Nano accepts it.

ss.png

And so does emacs.

ss.png

I assume you are using gcc to compile and that understands it perfectly as well.

root@rishi-messaround:~/temp# gcc main.c -Wall -o main
root@rishi-messaround:~/temp# ./main
Hello, World!

What's the editor/compiler that doesn't take it, got me really curious now lol.

0
0
0.000
avatar

lol 😂🤣... My bad... Thanks for showing me this! I appreciate... I know better now

0
0
0.000
avatar

I love how colourful your editor is 😍... Mine is looking too boring for my liking. What system are you using?

0
0
0.000
avatar

I use termius and that has color support. Their paid plan has SFTP and much much more which I use daily and is 100% worth the $100/year cost.

0
0
0.000
avatar

From what I've read here, the only thing I could learn was the meaning of variable and how it is and that's because it has a note on it, hehe.

I think you are really learning a lot as the day goes by and I pray for more understanding to you 😊.

0
0
0.000
avatar

Most of the things here, I got to understand them when I was learning JavaScript. It was my first programming language... Let's say my first love.

But I would still go back to it, definitely. I just choose to take a detour first before coming back to it..

0
0
0.000
avatar

Oh wow, I've heard of Javascript but I don't really know much about it. Well I'm sure when I get a laptop I will be able to easily understand some of these things.

0
0
0.000
avatar

Thank you for the piece my friend. i added alot to my knowledge from this.
You deserve my reblog

0
0
0.000
avatar

This is a nice one. Bringing C programming to the Blockchain. I'm sure most persons would find it useful, if not now, maybe in the future.

I like how you explained almost to a lay man's level.

0
0
0.000
avatar

Lol!!!! Thank you very much.. Writing this article helped me in solidifying my knowledge. It made me to rethink on what I already know, search and make confirmation of things and even better understand things the way I previously did. It was fun writing this article for me.

If I don't write it or break it down to a lay man's level.. then I don't probably yet understand things at the ground level yet.

0
0
0.000
avatar

Exactly so! Teaching others what you know is the best of learning more. Like if you're worried about being a good teacher, you will be concerned about making things as simple as possible for your learners, and as a result, you'll need to dopre research, verify your knowledge and present your lesson in a more practical way. Which you've done here! It's quite commendable bro.

One day, I'll do like you.

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar
(Edited)

The data type I hate the most is char, coz that's what you use to create a string in C. Unlike other languages like python and JavaScript, C doesn't have the string data type, you will have to create an array of characters to represent a string which can be a serious pain in the neck because you will have to deal with allocating and reallocating memory. C is a great language for learning the basics of programming but having to deal with memory management can be very frustrating sometimes, thank God other languages took care of that problem on their own

0
0
0.000
avatar

I would definitely get there! I have not gotten much knowledge in C yet but soon I would get to feel that pain in the ass you are talking about.

0
0
0.000
avatar

I would definitely get there! I have not gotten much knowledge in C yet but soon I would get to feel that pain in the ass you are talking about.

0
0
0.000
avatar

Another programming content is here! It's obvious you are digging deep into this course and you are also progressing in this learning phase.
At least I grabbed what variables is and data modifiers.

This will surely be of many benefits to engineering students here👌
Thanks for sharing 👍

0
0
0.000
avatar

Boss this is a really nice and well detailed post, even without knowing about C programming, one can come here and learn from this.
This variables and data types here are quite different from other languages although, float & Int are common in other languages and the way you comment.
This is a journey of a lifetime bro, learning and educating others at the same time isn't an easy task, kudos to you sir I wish you more strength and grace.

0
0
0.000