This is the second part of a 12 part series of blog posts about programming in the C language. This tutorial will guide you through learning C programming, but since there are already a lot of different tutorials out there on the web to learn the basics of programming I’ll not give you yet another tutorial on the exact same thing. These tutorials will instead give you a small description of the different pieces making up the C language. It will give you some links to learn the absolute basics and then continue with a discussion. My hope is that this discussion will enhance your learning, giving additional tools to understand the content on the other sites. The discussion will also hopefully give you additional value in understanding more about the possibilities, limitations, and usefulness of different concepts in different situations.
Table of Contents
The 12 parts are made up of the following posts:
- Introduction to C programming
- Data Types
- Allocating Memory
- Error Handling
- Other Goodies
Data types in C is quite easy, I think it’s a good idea to start from the bottom-up in this particular case. So in the memory space, there are a lot of data stored in bits. Bits are simply put a container which can hold a 1 or a 0. In programming, we usually regard 1 as a “true” value and 0 as a “false”. This allows us to use different kinds of logic in programs. But in many cases, we want to be able to hold different values than true or false values. To solve this we bundle the bits into different sizes and we call each group of 8 bits for a byte.
A byte can hold a number of the maximum size 2^8 = 256 (counting in the binary system). In the early days of computing 8 bits was a kind of big chunk so the scientists of the days decided that this number of characters would suffice for communication, and so they created the ASCII table which has 256 different symbols. Each value of the 1 byte entity was therefore also given a symbolic meaning and could be used to encode messages.
In C we call this the char-type, it is 1 Byte (8 bits) large and is usually used to store characters (even though it, in reality, holds values).
When computing numbers we often need to be able to use values larger than 256, therefore in C we also have the integer-type. This is a type which use 4 Bytes (32 bits) to store large values, up to a value of approximately 2 billion and down to -2 billion. This is usually sufficient but there also exists a long integer-type which can be used to store even larger numbers.
One of the main drawbacks of the integer type is that it can’t store digits. But there is a solution to this as well. The float-type of 4 Bytes (32 bits) can be used to hold floating point numbers. If you need to be able to hold large or super precise floating pointer numbers then you can also use a double-type on 8 Bytes.
To learn more about data types read on tutorialspoint and watch this videos.
I’d say this about covers it for the basics of data types. And there is not that much more to cover here in terms of additional information which isn’t also a part of the later post in this C language tutorial. But of course, I still think we can have a small discussion.
So now you know how to create variables to store information. But there are actually a few more keywords that can be interesting to know about. These are the const, static, signed and unsigned.
Signed variables contain values in the range negative to positive.
Unsigned variables contain only positive values.
The difference in these from a top-down perspective is that signed variables are more versatile and will allow you to use both negative and positive counting and enable you to store those results. Unsigned variables will only allow you to count if you are sure that the result will not be negative, but gives the benefit that larger numbers can be used. Signed variables are typically used for most arithmetic operations while unsigned variables as an example could be used when counting the length of a list.
Const (or Constant) variables means that the value can only be set once. A variable which uses the const keyword can only be set during its initialization. This has the benefit of enabling you to know that the value will never change and your programming environment will warn you if you try to change it. This is good in any situation where a value can only be one thing.
Static variables are only initialized once. Why you’d want to do that isn’t very clear as of yet but as you’ll see when we get to functions and scope, the static variables can be very useful.
There are some more interesting things to know about types in C. You can chain them, meaning that you can let one variable be the value of another which has the value of another which has some value. Then all of the variables in this chain will get the rightmost value. This can be used to reduce the amount of space needed to give many variables the same value.
There is, of course, more to be known about variables but since this comes in combination with later concepts I’ll stop the discussion here. Did you like the post and/or did you think that I left out something important which you want to add or ask about? Like the blog post and leave a comment below, spread the word about it chats or social media, I’ll really appreciate it!