Category: Programming

Progam Writing and Coding Style and Commenting Code

Your Coding Style should contain Comments

/* comment */


/*********************************************************************
* Program Name: description *
* Author: Name Surname *
* Purpose: Reason for program *
* Usage: What to Expect *
*********************************************************************/

Commenting:

Commenting in HTML

Commenting in Bourne Shell(unix, Shell, perl, Python, Ruby)

# comment

Commenting in Java, C, C#, C++, PHP

/* comment */ or // comment

Commenting in VB

'comment

Basics of Writing Computer Programs

Overview Writing Computer Programs:

Programs start as a set of instructions written by humans. They start as an idea, which are put into text as a source file, containing source code. The source file is transformed into an object file by the compiler. Then a program called a linker takes the object file and combines it with predefined routines from a standard library and creates an executable.

Wrappers for Computer Programs:

Fortunately you don’t need to run compiler, linker etc individually they are wrapped into one tool. Some editors go further to create an IDE (Integrated Development Environment) that contains an editor, compiler, linker, project manager and debugger.

Actually Writing Computer Programs:

1. Create a place for your program:

mkdir hello

cd hello

2. Write the code

vi hello.c

type:

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

3. Run the compiler

Generic Unix C Compiler

cc -g -ohello hello.c

-g : enables debugging

-ohello : tells compiler program is to be called hello

GNU C Compiler

gcc -g -Wall -ohello hello.c

-Wall : turns on warnings

4. Execute the Program

hello

or

./hello

What is C?

C is a programming language, it is a method of communicating with a computer.

C is a particularly flexible language and has been used for micro-controllers, operating systems, applications and graphics programming. An improved C language called C++ has been invented and it does lots of things you don’t know about like calling constructors and destructors for variables. So C++ does some things automatically while C doesn’t.

You can’t speak english to a computer as there is too much ambiguity. However, there is a group of people who spend their time trying to write precise English. They’re called the government and the documents they write are called government regulations. unfortunately in their effort to make regulations precise the government has made them unreadable.

Machines use binary as their language. In order to give instructions (translate) to this language we use assembly language. Translation was tedious so a program was used for this task, called an assembler. The assembler made it easier for programmers to understand but harder for machines to use.

Thereafter a number of higher level languages were developed, such as FORTRAN, COBOL and PASCAL.

Brief History of C

In 1970, Dennis Richie, created a new language called C. The only goal of this programming language was to program operating systems. The language was simple and flexible and soon was used for many different types of programs. The language did not get in the way of the programmer, anything could be done using the proper C construct. The compiler was portable and widely available.

“C with Classes” was developed in 1980 and became C++, thereafter ‘C++ with the bugs fixed’ was called Java.

Dennis Richie the inventor of C
Dennis Richie the inventor of C

How C Works

C is a bridge between the programmer and the raw computer. The idea is to let the programmer organize a program in a way that can easily be understood. The compiler then translates the language into something that the machine can use.

Programs consist of data and instructions. The programmer should impose his organization on the computer.

Data is stored as a series of bytes and declarations are used to describe the information:

int total;

Tells the memory to store an integer named total.

int balance[100];

For more complex data types eg. a rectangle with a width, a height, a colour and a pattern.


struct rectangle{
int width;
int height;
color_type colour;
fill_type fill;
};

instructions:

area = (base * height) / 2.0;

An arithmetic assignment statement.

Standard control and selection statements such as if, switch, while and for are also present. Groups of reusable functions can be combined into a library, so you can borrow from the library with qsort for example.

All these structures are organised to benefit the programmer, not for the computer.

The source of this is not my own, it is from the book: Practical C Programming