Copyright © 2010 K-Tutorials.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

The contents are updated or revised periodically; the document version will be changed based on the update or revision of the contents.

Document version: 1.0

Thursday, October 27, 2011

Programming Fundamentals

Programming

Programming is a process of writing instructions in a language that can be understood by the computer so that a desired task can be performed.The computer can understand only machine language and programming in machine language is very difficult job or tedious.

Programming Languages

A programming language is an artificial language designed to express computations that can be performed by a machine particularly a computer. Programming languages can be used to create programs that control the behavior of a machine. Many programming languages have same form of written specification of their Syntax (form or format) and Semantics (meanings).

A programming language is a notation for writing programs, which are specifications of a computation or algorithm.

All programming languages have some primitive building blocks for the description of data and processes or transformations applied to them. Theses primitives are defined by syntactic and sematic rules which describe their structure and meaning respectively.

Categories of Programming Languages
  • Machine Language
  • Low-Level Language
  • High-Level Language

Machine Language

This is the basic language understood by a computer. This language made up of ‘0’s and ‘1’s (that is actually represents the two signal voltages; because computer is a digital electronic device; any digital electronic device uses only two state of signals or voltage levels for their operations; and these devices are operated by digital signal processing (DSP)). A combination of these two digits represents characters, numbers and/or instructions. Machine language is also referred to as binary language. All instructions’ are encoded in binary and they can be executed by the computer directly.

E.g. For machine language representation in binary form is.


1001101010100011001110110011011110110101011111100110

Low-Level Programming Languages

Low-level language is a programming language that provides little or no abstraction from a computer’s instruction set architecture. A low-level language does not need a compiler or interpreter (these are translators used for converting high-level language programs into machine language) to run; the processor for which language was written is able to run the code without using either of these. Assembly language (low-level language) is one step higher than the machine language. Here every machine language instruction is given a symbolic name so the program can be written easily using these names instead of binary instructions.

E.g. Low-level Assembly language instructions

MOV B, C

ADD B

High-Level Programming Languages

A high-level programming language is a programming language with strong abstraction from the details of the computer. A high-level language isolates the execution semantics of computer architecture from the specification of the program, making the process of developing a program simpler and more understandable with respect to low-level language.

E.g. Examples of High-level programming languages are C, C++, JAVA .etc.

System Software

System software assists in the functioning of a computer system and includes the “Operating System (OS)”, Assembler, Interpreter, Compiler, Linker and Loader.

The Operating System is the interface between the user application and system hardware (or Interface between the user and the computer). The Operating System performs important functions like memory management, protection and security.

Translators in Programming Languages

Translators are special type of programs used in converting programs written in various programming languages (low-level and high-level languages) into machine language. There are basically three deferent types of translators; they are:

a. Assembler

The “Assembler” is a translator that converts assembly language code into machine language.

b. Interpreter

The “Interpreter” is a translator that converts source language (high-level language) code or instructions (programs) into executable code (machine language) in a step by step manner. An instruction converted by the interpreter is executed and then the next instruction is taken up for processing.

c. Compiler

The “Compiler” is a translator that coverts the source language (high-level language) code or instructions (programs) into executable code (machine language). The deference between the compiler and the interpreter is that the compiler converts the entire program at once whereas the interpreter does so in parts (one by one). The compiler output is either an assembly program (low-level program) to be run through the assembler or it is in the form of directly executable “Object Code” (in machine code). The object code generated could either be stored as an object module or then loaded for the execution when needed, or it can be loaded directly for immediate execution.

Hence, both interpreter and compilers are used to convert high-level languages into machine language.

Linker

A “Linker” is special type of program sequence performs the important task of linking together several object module. The need for a linker arises when a particular program is made up of several object modules. The original source program may be in any high-level language. Once they are converted into machine code, they must be linked together to be executed as a single program. This task is performed by the linker. The linker has to resolve external references made in the individual modules. The final object module thus created has to be loaded into memory to be executed by the processor.

Loader

The task of loading the linked object modules is performed by the “Loader”. Loader is special type of program sequence that loads the linked object modules by the linker for execution. There are two types of loaders depending on the way the loading is performed: “Absolute Loaders and Relocating Loaders”. Absolute loaders load the executable code (object code or machine code) into memory location specified in the object module. Relocating loaders on the other hand, load the executable code (object code or machine code) into memory locations which are decided at load time. The relocating loaders are performed as they permit the executable to be loaded in any location in the memory. However, they require additional information to be stored in the object module. This is obvious, since the information regarding the relative addresses of the memory locations being accessed in the module has to be stored within it.

Steps Involved In Writing a Program
  1. Write a problem statement for what you want to do
  2. Write an outline, if it is complex
  3. Write up a section in pseudo-code (English instructions)
  4. Write the section in the computer language of your choice (this is called things like source code or a program or a sub-program)
  5. Compile the program. This means use a program called a compiler to translate your program into something the computer can understand (special numbers, often called machine code or assembly language).
  6. Run your program and see if it makes the computer do what you wanted.
  7. If not, fix the program, and compile and run it again.

Software Life Cycle

Software Engineering is defined as a streamlined approach to design and maintenance of computer program using tools that help manage their size and complexity. A collection of related programs are known as the Software.

1. Problem Statement:

The problem statement should be clear and unambiguous. If the statement is not precise and clear, the design and implementation will be erroneous.

2. Problem Analysis and Design:

This is the most crucial step in writing a program. The problem should be analyzed to find the most efficient way in which it can be solved. An “Algorithm” to solve the problem is developed based on the analysis. The algorithm may be expresses as a “Flowchart” or in the form of “pseudocode” (an outline of a program written in a form that can easily be converted into real programming statements). The design specifies the types of data structures used and defines the relationship among the variables.

3. Analysis of Algorithm:

The algorithm should be analyzed to check if it performs the desired action using a reasonable amount of time and memory.

4. Coding:

This step involves coding the algorithm using an appropriate programming language to form a program. The choice of a programming language depends on several factors. Some of them are the availability of efficient translators, programming constructs required by the algorithm, portability constraints, and proficiency of the programmer in the programming language .etc.

5. Documentation:

Documentation is essential to make the program easy to understand. It informs the reader about the working of the program, the mode of interaction and purpose of variables used. A person who reads the program should be able to understand and modify the program using the documentation.

6. Testing:

The program should be tested thoroughly using all possible types of inputs. Testing detects inconsistencies (if any) in the program.

7. Maintenance:

The program may require changes depending on its application. The changes and updates required are performed during maintenance

Some details about programs and their creation process

As we already seen that the steps involved in writing a program. Writing a program in any programming language typically depends on the availability of efficient translators, programming constructs required by the algorithm, portability constraints, and proficiency of the programmer in the programming language .etc.

After selecting a suitable programming language to produce the program….

Source Code >> Object Code >> Executable Code (EXE)

Why Use C?

In today's world of computer programming, there are many high-level languages to choose from, such as Pascal, BASIC, and Java. But C stands apart from all these languages. This is due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used to write any complex logic program. The C language compiler combines the capabilities of a low level language with the features of a high level language. Therefore the language is suitable for writing both system software as well as business packages and other software. You will many compilers available in the market written in C.

  • Program written in C are very efficient and fast. This is due to its variety of data types and powerful operators. It is many time faster than BASIC. This helps developers in saving their valuable time.
  • C is a powerful and flexible language which helps system developers to deliver various complex tasks with ease. C is used for diverse projects as operating systems, word processors, graphics, spreadsheets, and even compilers for other languages.
  • C is popular among professional programmers for programming, as a result, a wide variety of C compilers and helpful accessories are available.
  • C is highly portable language. This means that a C program written for one computer system (an IBM PC, for example) can be run on another system (a DEC VAX system, perhaps) with little or no modification. Portability is enhanced by the ANSI standard for C, the set of rules for C compilers.
  • C’s another striking feature is its ability to extend itself. A C program is basically a collection of various function supported by C library (also known as header files). We can also add our own functions to the C library. These functions can be reused in other applications or programs by passing pieces of information to the functions, you can create useful, reusable code.
  • Writing C program with user-defined functions makes program more simple and easy to understand. Breaking a problem in terms of functions makes program debugging, maintenance and testing easier.

As these features shows that C is an excellent choice for your first programming language. But what is C++? You might have heard this term C++ and the programming technique called Object-Oriented Programming (OOP). Possibly you're wondering what the differences are between C and C++ and whether you should learn C or C++.

C++ is a superset of C, means that C++ is the new improved version of C for the latest programming needs. It contains everything C does, plus new additions for OOP concept. If you will learn C++ later, you will find that almost everything which you learnt in C is applicable in C++ too. In learning C, you are not only learning one of today's most powerful and popular programming languages, but you are also preparing yourself for other similar programming languages.

Another language that has gotten lots of attention is Java for its OOP and portability. Java, just like C++, is based on C. If later you decide to learn Java, you will find that almost everything you learned about C applies here.

All popular computer languages are dynamic in nature. They continue to improve their power and scope by incorporating new features and C is no exception. Although C++ and Java were evolved out of C, the standardization committee of C decided to add few features of C++/Java to C, would enhance the usefulness of the language. The result was the 1999 standard for C. This version is usually referred to as C99.