Lesson 1 - Introduction to the C language

C and C++ The C language Basics Introduction to the C language

Welcome to the first lesson of the C programming language course. We'll go through everything, step by step, from the very beginning to the more complex structures, pointers, and things like working with files. With a little patience and persistence, you will become a good programmer.

First and foremost, it'd be appropriate to mention that the C language is a big piece of programming history nowadays. In fact, it's still being used in practice, however, it's used for very specific purposes. C is often being taught in the academic world as the predecessor of today's programming languages. It won't do you any harm if you decide to learn it, however, if you're not explicitly told to "start from the C language", we think it's possible to and even better to start with a modern language instead. Some good candidates would be Java, C# .NET, or PHP.

Genesis of the C language

The C languages was designed by Dennis Ritchie, the creator of the UNIX operating system, way back in the 1920s. In fact, the entire UNIX kernel is written in the C language. This is probably why the language is so popular. Also, this fact alludes to the extreme performance of the language. The C language was also ported to most platforms and almost all modern languages evolved from the C language. These languages are often referred to as "C-like" since they're based on C and their syntax looks like C as well.

To fully understand the C language, we'll have to look to the past and get a good understanding of how programming languages have evolved over the course of time. Doing so will enable us to understand how C works, and for what it's used nowadays. Last of all, we'll peek into the future and explain what the disadvantages of the C language are when compared to modern languages.

Evolution of programming languages

1st generation languages ​- Machine code

Computer processors can perform a limited number of simple instructions, which are stored as a sequence of bits, i.e. numbers. In most cases, the aforementioned instructions are written using the hexadecimal system, so as to make reading them less of a chore. However, the instructions are so limited, that all you can really do is sum up addresses and jump between instructions. As you may already know, in the world of programming, one does not simply add two numbers together. What we do, is look at the numbers' addresses in memory and then sum them up (which takes multiple instructions). Here's what adding two numbers would look like in the hex:

2104
1105
3106
7001
0053
FFFE
0000

The instructions are given to the processor in binary. This sort of code is extremely unreadable and is dependent on the instruction set of the given CPU. I assure you, it is extremely nauseating to program in this "language". Unfortunately, every program must be compiled in binary format so that it can be executed by a computer processor.

Assembly language principle

2nd generation languages ​​- Assembler

Assembler (ASM for short) is no simpler than machine code, but at least it's human readable! Here, the instructions have human readable text codes, so that people wouldn't have to memorize every single one of the number combinations. The instruction codes are later compiled into binary code. Adding two numbers up in ASM would go something like this:

ORG 100
LDA
ADD B
STA C
HLT
DEC 83
DEC -2
DEC 0
END

It's a bit more human-readable, but most people, including me, would still have no clue how this program works.

3rd generation languages

Third generation languages finally give a good amount of abstraction of how the program is seen by the computer. Rather than forcing us to adapt to the computer's arcane way of thinking, the languages focused a bit more on how we see the program. Numbers were then perceived as variables and code had an almost "mathematical-notation" sort of aesthetic.

Adding up two numbers in the C language would go like this:

int main (void)
{
        int a, b, c;
        a = 83;
        b = -2;
        c = a + b;
        return 0;
}

Pretty much anyone could assume what this program does just by looking at it. It sums 83 and -2 up, and stores the result in a variable named c. The main advantage third generation languages had over all of the previous languages was high readability. You can surely imagine what a programming revolution the C language caused, even though it wasn't the first of its kind.

Compiled languages

Compiled languages have their source code in a language that people can fully understand. The source code must still be translated into machine code so that it can be executed by the processor. This translation is provided by a compiler, which compiles the entire program into machine code.

Compiler language principle

Object-oriented programming and Garbage Collector

Object-oriented programming (OOP) is a feature which the C language unfortunately lacks. OOP enables us to make large programs extremely readable and maintainable by splitting them up into communicating objects. Most of the modern languages use OOP and although we can simulate some of its principles in the C language, we wouldn't be able to achieve all of its advantages. Our programs will be a bit faster without OOP, however, on today's computers and considering today's application complexity, source-code readability becomes more and more important than performance.

We run into a similar situation with the Garbage Collector, which automatically clears the memory used by our program. C doesn't have anything like that which makes it faster, but also more dangerous since the programmer has to clean up after themselves.

Some people say the C language is like racing a formula car. You wouldn't drive out in streets with that car, but you would bring it to a racing track when an experienced driver is present to drive it. For ordinary uses, an ordinary car (a modern language) is the way to go.

The C language specifics

Let's sum up some advantages of the C language:

  • The C language is extremely fast, and therefore it's mainly used for creating operating systems or higher programming languages (for example, PHP or C++ languages are written in the C language)
  • The C language is completely multi-platform, so it's available on most operating systems
  • The C language's syntax is similar to most of today's programming languages' syntax.

However, the disadvantages outnumber them:

  • C is not a high-level language, although, some old articles or books say so because it once used to be. From today's point of view, it's more of a low-level language, meaning a language of high performance but low comfort for the programmer. That's why it's not a good choice for common projects which don't require extreme performance. In these cases, C's successor, C++, is a more appropriate choice.
  • C cannot work with strings, which is bypassed in a very uncomfortable way, by using char arrays and dynamic memory allocation.
  • The C language is an unmanaged language with direct memory access. This means you can break a program by getting the least bit distracted, and the program part which broke the whole thing might be completely random, i.e. very hard to find.
  • The C language does not contain any standard graphics library, however, there are third-party libraries for it.
  • C does not support object-oriented programming, but it can declare structures and header files which can simulate some of OOP's advantages.

Now, we know what we're going to work with. In the next lesson, Installing NetBeans and the C compiler, we'll install the necessary tools to create our first C program.


 

 

Article has been written for you by David Capka
Avatar
Do you like this article?
1 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Activities (5)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!