Introduction to C Programming



At the very base level, a C program to state the words "Hello, World" is as follows:

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

To write the program we need a text editor. On Linux we have many options, it is simply a matter of choice. Here at Linuxfx Computing we write both C and shell programs in the Nano editor.

To ensure you are using bash, type in the terminal:

sudo dpkg-reconfigure bash

Check with the command:

echo $SHELL

...the output should be /bin/bash

In Bash type in:
nano hello.c

Enter the text of the program into the editor and save with ctrl+o (enter).

Exit Nano with ctrl+x

Now we compile the program we have written using gcc:

gcc -o hello hello.c

If you have made an error, this is where it will show up. Go back to your program and re-check 'all' you have written, simple errors are easy to make and gcc will take no prisoners if you miss anything off your program.
Once this is compiled we need to make the code executable:

chmod +x hello.c

Then we get to actually run the program:

./hello
That's it, you have written, compiled and executed your first C program!


.... okay, let us now go a step further.

In Bash, we now need to create a new folder for our programs and create a path so we can execute directly from the shell. Type the following into the command line:

mkdir bin

PATH=$PATH:$HOME/username/bin

Now copy your hello file into /home/username/bin  *please note, 'username' will be your username.

cp hello /home/username/bin

Your hello file will now be within the /bin folder.

Now we have a /bin folder and a path to the script from within Bash we can simply type in:

hello

If you do not have your /home/username/bin folder in your path, the programs you write will need the prefix of ./ as in ./hello


As we shall see, this folder can be utilised for scripts written in C or Shell.


C Language - Why use it?

C is a very powerful and compact language which allows you to write programs that specify exactly what you want your computer to do.

The primary design of C is to produce portable code while maintaining performance and minimizing footprint, as is the case for operating systems or other programs where a "high-level" interface would affect performance. It is a stable and mature language whose features are unlikely to disappear for a long time and has been ported to most, if not all, platforms.

For example, C programs can be compiled and run on the HP 50g calculator (ARM processor), the TI-89 calculator (68000 processor), Palm OS Cobalt smartphones (ARM processor), the original iMac (PowerPC), the Arduino (Atmel AVR), and the Intel iMac (Intel Core 2 Duo). While nearly all popular programming languages will run on at least one of these devices, C may be the only programming language that runs on more than 3 of these devices.

One powerful reason is memory allocation. Unlike most computer languages, C allows the programmer to write directly to memory. Key constructs in C such as structs, pointers and arrays are designed to structure and manipulate memory in an efficient, machine-independent fashion. In particular, C gives control over the memory layout of data structures. Moreover dynamic memory allocation is under the control of the programmer, which inevitably means that memory deallocation is the burden of the programmer.

Languages like Java and Perl shield the programmer from having to worry about memory allocation and pointers. This is usually a good thing, since dealing with memory allocation when building a high-level program is a highly error-prone process. However, when dealing with low level code such as the part of the OS that controls a device, C provides a uniform, clean interface. These capabilities just do not exist in other languages such as Java.

While Perl, PHP, Python and Ruby may be powerful and support many features not provided by default in C, they are not normally implemented in their own language. Rather, most such languages initially relied on being written in C (or another high-performance programming language), and would require their implementation be ported to a new platform before they can be used.


History

The roots of C programming go back to 1969 - 1973, when it was developed by Dennis Ritchie at Bell Telephone Labratories for use with the Unix operating system. It was called C language after evolving from the B language which was a stripped down version of BCPL, or to give its full title - Basic Compiled Programming Language.

The portability of UNIX was the main reason for the initial popularity of both UNIX and C. So rather than creating a new operating system for each new machine, system programmers could simply write the few system ­dependent parts required for the machine, and write a C compiler for the new system.

Thereafter, since most of the system utilities were written in C, it simply made sense to also write new utilities in that language.

By 1973, the C language had developed enough and was powerful enough for the Unix kernel to be re-written in C and was implemented on a PDP-11. Prior to this, the B assembly language had been used on the PDP-7.

PDP-7

pdp 7 computer


PDP-11

pdp-11 computer


In 1978, Brian Kernighan and Dennis Ritchie published 'The C Programming Language', a book which became the standard publication about C programming for many years. In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C.

In 1989, the standard was ratified as ANSI X3.159-1989 "Programming Language C". Revision names are sometimes referred to such as C89 or C90, though all are referring to the same C language.
Since the ANSI specifications, ANSI C has been in regular use and the more recent publications have this wording on the cover. See Amazon link HERE.
Brian Kernighan is also accredited with being the person to coin the phrase 'wysywig' or 'what you see is what you get'.



powered by linux