This article is a continuation on expressions that can be constructed in the C language. This article will focus on expressions involving operators. If you remember, expressions are pieces of code that are executed right away to yield some kind of result, as opposed to a statement which may affect an entire program’s structure.
By incorporating bitwise operations in its core language, C provides a way to program low level processor specific operations inside a more structured program (as compared to assembly). C lets us perform ands, ors, exclusive ors, and one’s complement operations to chars and ints.
Being able to compare the values of variables is extremely important in any C program. Later on we’ll cover how we can alter the flow of a program depending on the results of relational and logical operators, in essence creating programs or processes that can somewhat “think” for themselves. C provides an AND, OR, and NOT operators, but from these operators we can easily build any kind of more complex boolean logic operation, as we have done above with exclusive or (XOR).
C includes the basic arithmetic operators, including assignment, division, multiplication, addition, subtraction and modulus. When you operate on variables and other literals you have to be careful of your type conversions that C automatically does for you, lest you lose some of the data you are trying to protect.
What if ther was a way to name a certain type, and then use that name across your program? That way, if that particular type needed to be changed, say to be an int instead of a char, you could specify that in one place. Well, that’s where the typedef statement comes in. However, typedef’s can be a mixed bag, if used too much you acquire obfuscation in your code, but if used to little, you may end up having to edit your code in multiple places; or you may end up with code whose purpose is difficult to understand.
Enumerations allow us to specify identifiers for integer values without having to specify a long list of global variables or constants. We also covered unions, which are a bit more esoteric, but useful anyway. Sometimes we need to access an array of long ints as individual char bytes, with a union you can do that! Lastly, we rounded-up with bit-fields: a C programming built-in that allows us to identify and work with specific bits by name.
Structures are ways to encapsulate related data, presumably, under one data type. This allows us to organize data that is related to each other into one place. Otherwise, all of our data would have to exist in separate variables, and as we programmed we’d have to remember how it call connected ourselves. This is truly prone to drastic error, and memory wise is convoluted.
Literals are very important in programming, as they are one way to get data into a program. Assigning literals to variables allows us to specify a number of things such as the number of times we’re going to perform an operation. Using literals in various parts of the program allow us to specify how large an array is going to be. Using string literals we might hard-code (that’s when you put data that the program uses straight into the program code) an array of error messages that would be output when certain errors happen.
Pointers and arrays are closely related in C. Using indexes and pointers to access the contents of an array are simply two sides to the same coin. They offer two perspectives that aim towards the same goal. With pointer arithmetic (which we covered in the article on pointers), you can access each element in the array one after another or randomly. With array indexing you can specify a short hand number that is easier to understand and read but does the same thing.
Pointers at this point in the article series may seem like more of a pain that a boon, but they are very powerful when it comes to constructing articulate programs. Pointers allow us to pass the memory addresses of specific objects around in our programs, and to modify memory in various places.
Arrays give us a wealth of opportunities to creatively solve our problems, and I hope I was able to elaborate on more of their functioning in this article. They can be confusing at first, particularly multi-dimensional arrays, but they are simply sequences (of seqences of sequences…). Thanks for reading
In C variables have to be of a specific data type. Luckily, C defines only 5 basic data types that it works with, so it’s not too difficult to choose what you want. All the data types are covered in this article, including their size modifiers unsigned, signed, short and long.
I’m not personally huge on modifying or getting my IDE to do lots of bells and whistles, but simply having a central program and place to debug, test, compile, and write your code is very powerful for your productivity. Add on that basic code inspection tools, error checking, syntax highlighting, file tracking, version control, and you got yourself a tool I’d use any day.
Linkers are essential pieces of the software development process and allow programmers to separate out their code in meaninful ways. They also allow the use of programming libraries, code that has been generated to be used by other programs. The C Standard Library is an example of such a library, allowing standard C programs to link to its code to perform such operations as general input/output. If we did not have a linker in our compilation process we’d have to write all our programs as one giant monolithic file.
This isn’t THEE only memory map of every program everywhere, but it is generally the memory map for most programs most average programmers create, being imperative. In programming languages like assembly or C, you work with this map pretty directly, but in more abstract or higher-level languages such as Python or PHP, many of these memory issues and placements are taken care of for you by the interpreter or compiler.
You will find some individuals who consider whether a language is usually interpreted or compiled split languages into these categories. However, the truth of the matter is quite the opposite. Whether a language is compiled or interpreted is actually an independent choice from the nature of the language itself. Any language can be interpreted by what is known as an interpreter, or compiled by what is known as a compiler.
In this article we cover the C programming language: where did it come from, how it relates to other languages, what can it do, what doesn’t it do, and the future of the language. C is a powerful language any aspiring programmer would benefit from learning and mastering. Even today, in embedded systems, C is very much employed, and in that personal respect, important to building my robot.
With variables, data types, flow control, functions, expressions, statements, subroutines, and such under your belt, you are prepared to understand more fully just about any programming that comes your way from any of the popular general purpose languages out there. That’s really the core of what makes up programming today (granted you’re not using a more specialized language such as Prolog or LISP).
This was an introduction to the core aspects of electricity as most people conceive of it. I cover the ideas of electrical charge, sub-atomic particles and their relationship to current, and how current is believed to work.