Practical Computing Advice and Tutorials

Tue: 09 Aug 2022

Site Content


Technical Knowhow

Command Line Interface


C Programming

Why C?

I.M.H.O, the C programming language is as good a language to learn as any as it will introduce you to the concepts that you can apply to higher level languages, such as Python (technically a scripting language, but it was written in C). Also, C is native to the Linux OS, so the two kind of go hand-in-hand. This page will introduce some of the building blocks we're going to need with which to tackle a modest project.

Remember that, while many programming languages will allow you to shoot yourself in the foot, C will make sure that you never run short of ammunition!

C is a very powerful programming language and was devised in the early 1970s by Dennis M. Ritchie as an improvement to B. The full history of C is well documented by a number of websites, so it's a little pointless for me to do the same.

Link : https://www.codingunit.com/the-history-of-the-c-language#more-127

There are also any number of C tutorials written by people that have way more knowledge than I, so these pages will be no more than my own meandering journey rather than a tutorial. I'll be solving the "how to" as and when I get to it and using the gcc – GNU project C and C++ compiler, which comes with Linux distros, with which to work.

A good place to start: https://www.tutorialspoint.com/cprogramming/index.htm

A friend of mine recommended this site http://www.java2s.com/ which has a whole host of tutorials, including C.
{Cheers buddy}

Getting Started

The first thing to do with any programming language is a basic compile and run of a simple routine. Writing code is a simple matter of using a text editor. Some editors are more functional then others with auto complete and syntax highlighting options which can make for a better experience.

In essence, C code is a series of function calls. Let's start with the infamous "hello world program".

#include <stdio.h>
int main ()
    printf ("Hello, world!\n");
    return (0);

Save this as hello.c and compile with gcc -o hello hello.c then run it with ./hello

As basic as this seems, it serves as a template to all C programs as well as demonstrating that you're able to compile and run a routine. I encourage you to play about with this simple code and break it by removing a ; or comment out the #include line by prefixing the line with // just so that you can see how the compiler reacts. Fully understand what the errors are and how they are reported so that you'll better understand any basic error reports encountered as you develop routines for yourself. Note: the indents are for styling and readability, having no effect on the object code that's produced.

So, what we did there was to populate the function main() with another function called printf() to which we passed the string constant argument Hello, world!\n. All C programs have to have the main() function, which is then used to call other functions. That's really all there is to it; create functions that do whatever you need and order them in the correct sequence. The #include line fetches a library, in this case it's the 'standard input/output dot h' library. That's where the printf() function has been defined. You may want to have a look at it, and you can. One other thing to note here is the \n at the end of the string. This is called an 'escape sequence', the \ part is called a 'specifier'. Escape sequences are used to output, or 'echo', control characters. That sequence is defined as 'echo a newline'. Try leaving it out, then re-compile the code and run it so that you can see what the difference is.

Other control character escape sequences include...

Mark  Meaning
\n Force a newline
\t Horizontal Tab
\b Backspace
\r home (return) the cursor 
\f Form-feed a printer
\' Echo '
\" Echo "
\\ Echo \

Another specifier you'll find used is the % symbol, which is used to format numbers, characters, and strings.

Compile and run the following code...

#include <stdio.h>

/* The Declarations */
int x, y, w;

    x = 1;  /* Assignment */
    y = 0;  /* Assignment */

    printf("\n x is set to %d and y is set to %d\n", x, y);
    printf("\n !x is %d, !y is %d\n", !x, !y);

    x = 15;  /* Assignment */
    y = -20;  /* Assignment */

    printf("\n x is now set to %d and y is set to %d\n", x, y);
    printf("\n ~x is %d ~y is %d\n",~x, ~y);

    x = 3;  /* Assignment */

    printf("\n The value of x is now %d\n", x);

    w = ++x;  /* Assignment */
    y = x++;  /* Assignment */

    printf("\n w = ++x is %d and y = x++ is %d\n", w, y);

    printf("\n The value of x is now %d\n\n", x);

    return (0);

We have a few things going on here, so let's take them one at a time. First we made three declarations int x, y, w because all variables must be declared before use. This is telling the compiler that we're going to use three integer values and we're going to refer to them as x y and w. We then assigned the value of one to x and zero to y. We then made a printf() function call, passing to it a string argument, in which we created the formatting needed to display the values of x and y. This is what the %d sequence is doing. It's telling the compiler that 'here, is going to be a denary value'; one %d for each of the variables listed at the end of the printf() function. This is a table of the formatting marks you'll likely come across.

Mark  Meaning
%d Signed Denary number
%u Unsigned Denary number
%o Unsigned Octal number
%x Unsigned Hexadecimal number 
%c Single Character
%s String, ending with a Zero
%% Echo the % sign

The next printf() function call also sets out the formatted space for two Denary numbers, by way of the %d marks, but this time variables x and y have been modified with the ! symbol, or 'not' symbol, which returns a binary value of one or zero for 'true' or 'false'. This will only ever return 'true' if the value is zero, so !0 is the only value that will return 'true'.

After assigning new values to x and y, we display the values, again, via printf() function calls, the first of which simply show what the new valuse are, but the second one displays the results of a 'ones complement' operation which is what the ~ operation does. One of the uses for that operation is to quickly negate a number: ~x is equal to -x plus 1.

The final few lines demonstrate the difference between ++x and x++ which is that in the first insistent x is incremented by one before the value is assigned to w, but in the second insistent, y is assigned the value of x before x is incremented, the value of x being displayed by the final printf() function call.

Operator Classes
Mark  Meaning
* Pointer to a variable
& Address of a variable 
- Negate the value
! 'NOT' operation
~ Ones complement
++ Increment
-- Decrement

Binary Operations

"Binary Operators" need two numbers with which to work, hence the term. These are mostly used for logical control, as in:

if (x == y) do something;

while (x != y) do something;

These are the ones that you'll likely come across.

Binary Operators
Operation  Action
* Multiply by
/ Divide by (integer result)
% Modulus; remainder of a division 
= Assign value
== Is identical to
!= Is not identical to
+ Add this
- Subtract this
> Is greater than
< Is less than
>= Is greater than or identical to
<= Is less than or identical to
&& 'AND' action
|| 'OR' action

Don't confuse these with "Binary Number Operators", which include things like the shift operators, such as << which is a 'left shift' operation and requires two numbers; the number that is to be shifted and the number of places to shift by. We're getting a little deep in the weeds for an introduction page, but as an example, this can be a simple way of multiplying a number by itself. Consider this statement:


What's happening here is that all the bits in the memory location of where '42' is being stored are shifted one place to the left. If we look at the binary representation of '42' and '84', it becomes clear:

42:~ 00101010
84:~ 01010100

With VERY careful planning, this kind of operation can be used to quickly move a stack pointer, but this is an advanced subject and not really a suitable topic for this page.

I've now covered the basics for starting out with C. I hope that you learned something and that you read and follow my first project.

As always, thank you for reading and please feel free to use my feedback page if you have any questions or comments.