UNDERSTANDING DATA TYPES AND VARIABLES IN C PROGRAMMING
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.
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:
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);
}
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.
Let's us print the next example....
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:
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...
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:
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:
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:
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
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..
Your content has been voted as a part of Encouragement program. Keep up the good work!
Use Ecency daily to boost your growth on platform!
Support Ecency
Vote for new Proposal
Delegate HP and earn more
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.
Yeah, I am running Ubuntu terminal on my windows system... I am using vagrant Ubuntu to do this!
https://twitter.com/Starstrings1/status/1569837050773532672
The rewards earned on this comment will go directly to the people( @starstrings01 ) sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.
You can also use
//
for single line comments. I find this a lot easier than the/* comment */
pattern for single line.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.
What editor do you use that doesn't accept it?
Vim accepts it.
Nano accepts it.
And so does emacs.
I assume you are using gcc to compile and that understands it perfectly as well.
What's the editor/compiler that doesn't take it, got me really curious now lol.
lol 😂🤣... My bad... Thanks for showing me this! I appreciate... I know better now
I love how colourful your editor is 😍... Mine is looking too boring for my liking. What system are you using?
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.
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 😊.
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..
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.
Thank you for the piece my friend. i added alot to my knowledge from this.
You deserve my reblog
Thank you too! I appreciate the reblog!
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.
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.
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.
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.
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
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.
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.
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 👍
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.